0% found this document useful (0 votes)
74 views4 pages

Homework Assignments - Week 2 (Flow Control) : Classroom

The document provides homework assignments on flow control concepts including: 1) Converting between numeric bases and string representations 2) Array processing like filtering, checking sorted order, and detecting consecutive repeats 3) String operations like word counting and formatting output 4) Algorithmic concepts like binary search, insertion sort, and merging sorted arrays The homework covers basic programming tasks as well as some classic algorithms.

Uploaded by

Cosmin Grosu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views4 pages

Homework Assignments - Week 2 (Flow Control) : Classroom

The document provides homework assignments on flow control concepts including: 1) Converting between numeric bases and string representations 2) Array processing like filtering, checking sorted order, and detecting consecutive repeats 3) String operations like word counting and formatting output 4) Algorithmic concepts like binary search, insertion sort, and merging sorted arrays The homework covers basic programming tasks as well as some classic algorithms.

Uploaded by

Cosmin Grosu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Homework Assignments - Week 2 (Flow Control)

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)

4. Check array sorted:


Write a function that checks whether a given array of numbers is sorted (in ascending
order) or not.

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(); 

7. Decimal to any base:


Starting with the solution for Exercise #1, write a more general function that receives a
number and a base (between 2..36) and returns a string with the number’s
representation in that base.
E.g:
decimalToBase(10, 2) -> “1010”
decimalToBase(10, 10) -> “10”
decimalToBase(10, 8) -> “12”
 
Note: To support bases higher than 10 you can use letters instead of digits, e.g. for base
16: A = 10, B = 11, …, F = 15, ...
For example:
decimalToBase(47, 16) -> “2F” 
decimalToBase(1295, 36) -> “ZZ” 
 
 

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..

9. Word count unique:


Given a phrase of words separated by space, count how many UNIQUE words it
contains.
Example: "aa bb cc aa bb" => 3 unique words
Hint: you will need a way to compare each word with the list of unique words detected up
until that moment, in order to decide if to increase unique words counter for this word or
not..

10. Count per word:


Given a phrase of words separated by space, write a method which returns the list of
unique words (case-insensitive) and for each one number of times it appears.
The returned value should be a string, with this format:
“<word1>=<count1>,<word2>=<count2>,...”
Example: "Aa bb cc aa dd BB dd" => "aa=3,bb=2,cc=1,dd=2”
Hint: you'll need to keep somehow a list of pairs of 2 separate values {word + counter};
for this, you may consider using 2 arrays updated/iterated in parallel - 1st one for the
words, and 2nd one for the counters (so a word is stored at some index i in 1st array,
and its counter is stored at same index i but in 2nd array)
 

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

12. Insertion sort:


Write a function that receives an array and returns a new array sorted by ​insertion sort.
More info: ​https://en.wikipedia.org/wiki/Insertion_sort

13. Merge sorted arrays:


Write a function that receives two ​sorted​ arrays and returns a new ​sorted ​array with
elements from both arrays.
(note: this is called the ​merge​ 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

You might also like