Question

In: Computer Science

Swift Code 4 only please var arr: [String] = ["a","b","s","c"] var dictionary : [String : Int]...

Swift Code 4 only please

var arr: [String] = ["a","b","s","c"]

var dictionary : [String : Int] = ["a":1, "b":2, "c":3, "d":4]

for i in arr

if(dict[i] = even)

print ("even number"

if else (dict[i] =odd)

print("odd number")

else

print("Does not exist")

Is there a way to compare the string array with the dictionary keys. Use the array to see if string exists as a key in the dictionary. Depending on the value of the key, it will print a specific code

Solutions

Expert Solution

In case of any query, do comment. Please rate answer. Thanks

Code:

//given array
var arr: [String] = ["a","b","s","c"]

//given dictionary
var dictionary : [String : Int] = ["a":1, "b":2, "c":3, "d":4]


//For loop to iterate over array
for i in arr
{
//condition to check whether array element exist in dictionary as a key. dictionary.keys gives a collection of keys
//based on key present or not, decide the code to print in corresponding if-else
if(dictionary.keys.contains(i)){
//! used to unwrap optional returned from dictionary values
if dictionary[i]! % 2 == 0
{
print("even number")
}
else
{
print("odd number")
}
  
}
else {
print("does not exist")
}
}

screen shot of code and output:

main.swift 1 1/given array 2 var arr: [String] = ["a", "b","s","c"] 4 //given dictionary 5 var dictionary : [String : Int] = ["a":1, "b":2, "-":3, "d":4] 7 8 9- //For Loop to iterate over array for i in arr { //condition to check whether array element exist in dictionary as a key. dictionary.keys gives a collection of keys //based on key present or not, decide the code to print in corresponding if-else if(dictionary.keys.contains(i)){ print("key \(i) exist in the dictionary.") 16 17 18 else { print("key \(i) does not exist in the dictionary.") } } input key a exist in the dictionary. key b exist in the dictionary. key s does not exist in the dictionary, key c exist in the dictionary. ... Program finished with exit code o Press ENTER to exit console.O


Related Solutions

Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
for C++ pointers: a. Given int arr [10], *ap = &arr[2]; what is the result of...
for C++ pointers: a. Given int arr [10], *ap = &arr[2]; what is the result of (ap - arr)? b. Given int val = 10, *x = &val, *y; y = x; *y=100; what is the output of cout << *x << *y;? c. Given int arr [10], *ap = arr; what element does ap point to after ap +=2; ?
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
"Write in swift code only please, Create a SwiftUI project named DogBreeds based on the Contacts...
"Write in swift code only please, Create a SwiftUI project named DogBreeds based on the Contacts app. The app should be named DogBreeds. The app will display 10 dog images and names in a list"
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT