Answer:
private void printArray(int[] arr){ //0
System.out.print("["); //1
for(int k : arr){ //2
System.out.print(k + ", "); //3
}
System.out.println("]"); //4
}
Explanation:
This method should get you started. Your teacher may want a different format or a different type of loop. If you haven't learned for loops yet don't use them that'd be suspicious ;)
What I did:
0: This is the method heading with one parameter for the 1D array. If you need to access it in a different class change private to public.
1: This prints the [ with no line break
2: This is an enhanced for loop to iterate through each element in a 1D array. Alternatively, a normal for loop looks like this:
for (int i = 0; i < arr.length; i++)
instead of retrieving the variable with the enhanced for, you'd have to use arr[i] instead of k in this scenario.
3: This prints the value in each pass of the loop along with a comma and a space.
4: This adds the ] with a line break when the loop is completed
Hope this helps you get started :D comment if you need further help
Answer:
You may nest while and do-while loops, but you may not nest for loops - False
Answer:
def typeHistogram(it,n):
d = dict()
for i in it:
n -=1
if n>=0:
if str(type(i).__name__) not in d.keys():
d.setdefault(type(i).__name__,1)
else:
d[str(type(i).__name__)] += 1
else:
break
return list(d.items())
it = iter([1,2,'a','b','c',4,5])
print(typeHistogram(it,7))
Explanation:
- Create a typeHistogram function that has 2 parameters namely "it" and "n" where "it" is an iterator used to represent a sequence of values of different types while "n" is the total number of elements in the sequence.
- Initialize an empty dictionary and loop through the iterator "it".
- Check if n is greater than 0 and current string is not present in the dictionary, then set default type as 1 otherwise increment by 1.
- At the end return the list of items.
- Finally initialize the iterator and display the histogram by calling the typeHistogram.