Homework Assignments - Week 2 (Flow Control) : Classroom
Homework Assignments - Week 2 (Flow Control) : Classroom
CLASSROOM:
0. Warm-up:
a) Write a method which, given a string and a non-negative int n, returns a larger string that
is n copies of the original string. Example: xTimes(“Abc”, 2) => “AbcAbc”
b) Given a number non-negative int n, return the sum of the squares of all numbers
between 1 and n. Example: sumSquares(4) = 1*1+2*2+3*3+4*4 = 30
c) Given a positive number n (type: int), compute and return factorial of n (defined as: n! =
1*..*n). It should work well at least up to n=16.
Try first to write an iterative version for it, then an recursive one (considering that: n! =
n*(n-1)! and 1!=1)
d) Given the (romanian) name of a day of the week, return a number showing its position in
the week, or -1 if name is invalid.
Try to use the switch block. Example: 'luni'->1 .. 'Duminica'->7, 'invalid'-> -1
(hint: check out also these useful methods of String class: toLowerCase, toUpperCase)
e) Given a string, count how many vowels (a,e,i,o,u) it contains. Try to solve it first without
using any arrays, then with arrays.
Hint: check String class for useful methods you may need here (like: toLowerCase(),
length(), charAt(), toCharArray()..)
f) Given an array of numbers (of type double), compute their sum and then the average
value (hint: when computing average, you may reuse the sum() method)
g) Given an array of numbers (of type double), find and return the max value (or special
value Double.NEGATIVE_INFINITY if cannot find a max value - when can this happen?)
h) Given an array of numbers, compute the sum of all numbers until first negative one is
found (excluding it)
i) Given an array of numbers, create and return another array which has same numbers
but in reversed order
j) Given an array of numbers, and some other separate number x, update the array so that
each initial number is replaced by its value multiplied by the fixed x value (note: the
method should return void, but affect the initial array directly - doing an ‘in-place’ update)
HOMEWORK:
1. Decimal to binary:
Write a function that receives a positive integer value and returns its base 2
representation as a string.
E.g. decimalToBinary(3) -> “11”, decimalToBinary(10) -> “1010”, decimalToBinary(127)
= “1111111”
(more info: https://en.wikipedia.org/wiki/Binary_number#Conversion_to_and_from_other_numeral_systems )
2. Array to string:
Write a function that returns a string representation of an array of doubles. Format the
numbers with two decimals. (hint: look up info on using String.format() for float numbers)
E.g [1.1, 2.345, 3.0] -> “[1.10, 2.34, 3.00]”
3. Filter array:
Write a function that takes an array of numbers and returns a new array with only the
odd numbers. (hint: test it for a few cases, using Arrays.toString() to display the content
of the returned array)
5. Consecutive repeats:
Write a function that takes an array of numbers and returns just the numbers that are
repeating on consecutive positions in the array, as a single String value (containing the
numbers separated by space).
E.g: [1, 4, 3, 4] -> “” ; [1, 4, 4, 4, 3, 5, 5, 4, 4] -> “4 5 4” ; [1, 4, 4, 4, 5, 7, 8, 7, 2, 2, 9, 9]
-> “4 2 9”
Optional:
6. Binary to decimal:
Write a function that receives a number’s string representation in base 2 and returns the
number itself as an int (in base 10).
E.g. binaryToDecimal(“11”) -> 3 , binaryToDecimal(“101”) -> 5
Hint: to iterate through the characters in a string, you may either use String.charAt()
method (+String.length), OR you may convert it to a char array and iterate over that (you
also have to convert each char to its corresponding digit, like ‘0’(=48) -> 0; you may
create a separate method for that ..) :
String numberInBinary = "110101";
char[] binaryDigits = numberInBinary.toCharArray();
String operations:
8. Word count:
Given a phrase (as a String value), count how many words it contains. Words are
considered to be separated only by space (one or more spaces)
Example: "Aa bb cc dd" => 4 words
Hint: you may use the split() method present on String to separate the words - search
online info about using it (some other useful String methods to check: trim, isEmpty).
Optional: read about regular expressions (in Java), and find a way to call String.split(..)
so that it will directly eliminate multiple spaces from words (so it will not generate parts
which are formed just of spaces), and see if you can get a one-line solution using this..
Algorithms
11. Binary search:
Write a function that receives a sorted array and a number and performs a binary
search on the array to check whether the element is contained therein or not.
Bonus: write two versions of the function: one iterative and one utilizing recursion
More info: https://en.wikipedia.org/wiki/Binary_search_algorithm
Optional:
14. Merge sort:
Write a function that sorts an array of numbers using the merge sort algorithm.
More info: https://en.wikipedia.org/wiki/Merge_sort , https://www.programiz.com/dsa/merge-sort