Practice Problem 1
File problem1.java contains an incomplete program. The goal of the program is to go through an array of integers, and print out all integers that are greater than or equal to 50.
Complete that program, by defining a print_large function, that satisfies the following specs:
- Function print_large takes one argument, called array, which is an array of integers.
- For each integer in the array, the function prints out that integer if it is greater than or equal to 50.
If you implement your function correctly, the program should print:
105
82
60
Practice Problem 2
File problem2.java contains an incomplete program. The goal of the program is similar to that of Problem 1: we want to go through an array of integers, and print out all integers that are greater than or equal to 50. However, here we do it a little bit differently: we do not hardcode the value 50 in the print_large function, but we pass it in as an argument.
Complete that program, by defining a print_large function, that satisfies the following specs:
- Function print_large takes two arguments, called array, threshold.
- Argument array is an array of integers.
- Argument threshold is an integer.
- For each integer in the array, the function prints out that integer if it is greater than or equal to threshold.
If you implement your function correctly, the program should print:
105
82
60
Practice Problem 3
File problem3.java contains an incomplete program. The goal of the program is similar to that of Problems 1 and 2: we want to go through an array of integers, and print out all integers that are greater than or equal to 50. However, here we do it in a different way, by writing two functions: keep_large and print_array_list
Define a keep_large function, that satisfies the following specs:
- Function keep_large takes two arguments, called array, threshold.
- Argument array is an array of integers.
- Argument threshold is an integer.
- The function returns an array list of integers. For each integer in the array, the function adds that integer to the result if the integer is greater than or equal to threshold.
Define a print_array_list function, that satisfies the following specs:
- Function print_array_list takes one argument, called list, which is an array list of integers.
- The function prints out every integer in the list, one integer per line.
.
If you implement your two functions correctly, the program should print:
105
82
60
Practice Problem 4
File problem4.java contains an incomplete program. The goal of the program is a little different from that of Problems 1, 2, 3. Here the data is stored in an array list, instead of an array. We still want to go through the data in that array list, and print out all integers that are greater than or equal to 50.
You should reuse the print_array_list function from the previous problem.
You should define a new keep_large function, different from your solution in the previous problems, that satisfies the following specs:
- Function keep_large takes two arguments, called list, threshold.
- Argument list is an array list of integers.
- Argument threshold is an integer.
- The function returns an array list of integers. For each integer in the array, the function adds that integer to the result if the integer is greater than or equal to threshold.
If you implement your function correctly, the program should print:
105
82
60
Practice Problem 5
File problem5.java contains an incomplete program. The goal of the program is to read integers stored in a file, and print out all integers that are greater than or equal to 50.
You should reuse the print_array_list and keep_large functions from previous problems.
You should define a new read_numbers function, that satisfies the following specs:
- Function read_numbers takes one argument, a string that specifies the name of a file.
- The function returns an array list of integers stored in that file. You can assume that the file contains only integers, one integer per line.
If you implement your function correctly, using file numbers1.txt, the program should print:
105
82
60
Practice Problem 6
Write a function reverse_lines(filename)
that:
- Takes as argument a string
filename
, specifying the name of the file.
- Reverses the order of lines in the file.
For example, suppose that, right before calling the function, file file1.txt
has these contents:
hello
today is
Tuesday
Then, after calling reverse_lines("file1.txt")
, file file1.txt
should have these contents:
Tuesday
today is
hello
Practice Problem 7
Write a function foo(int A, int B) that satisfies these specs:
- If A is greater than B, foo(A, B) returns -1.
- If A is less than or equal to B, foo(A, B) returns the sum of all integers N such that A <= N <= B.
For example:
- foo(5, 9) should return 35, since 5+6+7+8+9 = 35.
- foo(5, 3) should return -1.
- foo(5, 5) should return 5.
Practice Problem 8
Write a function print_special(String S) that satisfies these specs:
- It prints, on the first line, the letter at position 0 of S.
- It prints, on the second line, the letter at position 1 of S, repeated twice.
- It prints, on the third line, the letter at position 2 of S, repeated three times.
- And so on, printing on the N-th line the letter at position N-1 of S, repeated N times, until all letters of S have been printed.
For example, print_special("Texas") prints this:
T
ee
xxx
aaaa
sssss
Practice Problem 9
Write a function print_special_to_file(String S, String output_file) that creates a new file, whose name is specified by argument output_file, such that the contents of the file are the same as the output shown on the screen in the previous problem.
More specifically, the contents of the output file should satisfy these specs:
- The first line contains the letter at position 0 of S.
- The second line contains the letter at position 1 of S, repeated twice.
- The third line contains the letter at position 2 of S, repeated three times.
- And so on, the N-th line contains the letter at position N-1 of S, repeated N times, until all letters of S have been printed.
For example, print_special_to_file("Texas", "target.txt") creates a file called target.txt, whose contents are these:
T
ee
xxx
aaaa
sssss
Practice Problem 10
Write a function check_letters(String A, String B), that satisfies these specs:
- If every character of B appears at least once in A, the function should return Boolean value true.
- If there exists a letter in B that does not appear at all in A, the function should return Boolean value false.
- Letter comparisons should be case sensitive.
For example:
- check_letters("this is a test", "asteh") returns true.
- check_letters("THIS IS a test", "asteh") returns false (because letter 'h',
in lower case, appears in the second argument but does NOT appear in the first argument).
Practice Problem 11
Write a function check_ab(String S), that satisfies these specs:
- If S contains an equal number of A's and B's the function returns true.
- Otherwise it returns false.
In other words, the function counts how many times letter 'a' appears in S, (counting both upper and lower cases), and how many times letter 'b' appears in S (counting again both upper and lower cases), and checks whether 'a' appears as many times as 'b'.
For example:
- check_ab("hello") returns true, because there are 0 A's and 0 B's.
- check_ab("cat") returns false, because there is 1 A and 0 B's.
- check_ab("Barbados") returns true, because there are 2 A's and 2 B's.
- check_ab("ALL BREAD and butter") returns false, because there are 3 A's and 2 B's.
Practice Problem 12
Write a function check_ab_in_file(String filename), that satisfies these specs:
- Argument filename specifies the name of an existing text file.
- If that file contains an equal number of A's and B's the function returns true.
- Otherwise it returns false.
In other words, the function counts how many times letter 'a' appears in the text file, (counting both upper and lower cases), and how many times letter 'b' appears in the text file (counting again both upper and lower cases), and checks whether 'a' appears as many times as 'b'.
Practice Problem 13
Write a function find_N(int number) that satisfies these specs:
- If there exists an integer N >= 0 such that number = 2N + 3N, the function should return that integer N.
- Otherwise, the function should return -1.
For example:
- find_N(2) returns 0, because 2 = 20 + 30.
- find_N(4) returns -1.
- find_N(5) returns 1, because 5 = 21 + 31.
- find_N(10) returns -1.
- find_N(13) returns 2, because 13 = 22 + 32.
Practice Problem 14
Write a function subarray_sums(int[][] items) that satisfies these specs:
- items is an array of arrays of ints. Thus, each element of items is an array of ints.
- The function returns an array of ints called result, of the same size as items. In other words, the number of elements of result is the same as the number of arrays of ints contained in items.
- The value stored at result[i] should be the sum of all values of the array stored in items[i].
For example:
- subarray_sums({{10, 5, 3}, {3, 1}} should return array {18, 4}, because the sum of all values in array {10, 5, 3} is 18 and the sum of all values in array {3, 1} is 4.
Practice Problem 15
Write a function pick_elements(int[] items, int[] positions) that satisfies these specs:
- The function returns an array of ints called result, of the same size as positions.
- The value stored at result[i] should be the value stored in array items, at the position specified by positions[i]
For example:
- pick_elements({10, 5, 3, 20, 30}, {3, 1}) should return array {20, 5}. The result contains the values at positions 3 and 1 of array items.
- pick_elements({10, 5, 3, 20, 30}, {0, 4, 1}) should return
array {10, 30, 5}. The result contains the values at positions 0, 4, and 1 of array items.
Practice Problem 16
Write a function insert(int[] items, int position, int value) that satisfies these specs:
- The function returns an array of ints called result, whose size is 1 + the size of items.
- For any i such that 0 <= i < position, result[i] = items[i].
- result[position] = value.
- For any i such that position < i < result.size(), result[i] = items[i-1].
For example:
- insert({10, 5, 3, 20, 30}, 0, 11) should return array {11, 10, 5, 3, 20, 30}. The result is the same as the items argument, except that we have inserted value 11 at position 0.
- insert({10, 5, 3, 20, 30}, 4, 11) should return array {10, 5, 3, 20, 11, 30}. The result is the same as the items argument, except that we have inserted value 11 at position 4.
Practice Problem 17
Write a function a_after_star(String s) that counts and returns the number of times that character 'a' occurs AFTER the first occurrence of character '*'.
The function should return 0 if the string does not contain character '*' at all.
For example:
- a_after_star("ab*ab*ab") should return 2.
- a_after_star("a cat*chases after its tail") should return 3.
- a_after_star("a cat*chases after*its tail") should return 3.
Practice Problem 18
Write a function a_between_stars(String s) that counts and returns the number of times that character 'a' occurs AFTER the first occurrence of character '*' and BEFORE the second occurrence of character '*'.
The function should return 0 if the string does not contain character '*' at all. If the string contains character '*' only once, the function should return the number of times that character 'a' occurs betweeh character '*' and the end of the string.
For example:
- a_between_stars("ab*ab*ab") should return 1.
- a_between_stars("a cat*chases after its tail") should return 3.
- a_between_stars("a cat*chases after*its tail") should return 2.
Practice Problem 19
We have files like towns.txt, whose contents look like this:
cat1chicago9234
dog3dallas01
sheep2boston4392897
donkey6denver4921109
Each line contains:
- The name of an animal (which consists of lower-case letters).
- Then, a single numerical digit.
- Then, the name of a city (which, again, consists of lower-case letters).
- Then, more numerical digits.
Write a function print_towns(String filename) that satisfies these specs:
- Its argument is the name of a file containing lines of text as described above.
- The function prints, for each line in the file, the city name that appears in that line.
For example, applied to file towns.txt, print_towns("towns.txt") prints this:
chicago
dallas
boston
denver
Practice Problem 20
Write a function check_sorted(int[] array) that:
- Returns boolean value true if the array is empty.
- Returns boolean value true if the values of the array are sorted in ascending order.
- Returns false otherwise.
For example:
- If a1 = {20, 10, 20, 30, 30}, then check_sorted(a1) should return false.
- If a2 = {5, 5, 8, 10, 10, 12}, then check_sorted(a2) should return true.
Practice Problem 21
Write a function check_squares(int[] array), that satisfies these specs:
- If the array is empty, it returns false.
- If there exists an integer N such that the array contains both N and N2, the function returns boolean value true.
- The function returns false otherwise.
For example:
- If a1 = {1, 1, 1, 2}, then check_squares(a1) should return true.
- If a2 = {20, 10, 20, 30, 40}}, then check_squares(a2) should return false.
- If a3 = {20, 100, 2, 10, 20, 20, 20}, then check_squares(a3) should return true.
Practice Problem 22
Write a function count_equal(String filename, int col1, int col2), that satisfies these specs:
- Argument filename is the name of a CSV file.
- The function counts and returns the number of rows where the value at column col1 is equal (as a string) to the value at column col2.
- The function should be able to handle cases where each line may have a different number of values separated by commas.
- The function should be able to handle cases where some line in the file does not contain any values at column col1 or column col2.
For example, using file file1.txt:
- count_equal("file1.txt", 0,1) should return 2.
- count_equal("file1.txt", 0,5) should return 2.
- count_equal("file1.txt", 1,4) should return 1.
- count_equal("file1.txt", 1,3) should return 0.
Practice Problem 23
Write a function largest_average(String filename), that satisfies these specs:
- Argument filename is the name of a CSV file.
- The function computes, for each column, the average of all numbers appearing in that column, and returns the largest of all averages. To compute the average for a column, the function considers all values in the column that can be converted to double numbers, and computes the average of those values. If a column does not contain any numbers, the average of that column is considered to be 0.
- The function should be able to handle cases where some values cannot be converted to numbers. Those values should just be skipped.
- The function can assume that all rows in the CSV file have the same number of columns, there is no need to be able to handle files that violate this assumption.
For example, using file file2.txt:
- largest_average("file2.txt") should return 1032.25, which is the average of all numbers found in column 2 (the second-to-last column in the file). That column contains numbers 15, 2000, 2014, 100, and their average is 1032.25.
Practice Problem 24
Write a function max_length_position(String filename) that satisfies these specs:
- Argument filename is the name of a CSV file.
- The function should handle the case where different rows may contain different number of values.
The function returns an array containing two integers: the row and the column of the value with the largest length in the file.
- The function should follow the convention that rows and columns are numbered starting at 0.
For example, using file file2.txt:
- max_length_position("file2.txt") should return array {4, 2}, because the string at row 4 and column 2 of that file is "Wednesday", which is the longest string stored in that file.
Practice Problem 25
Write a function print_combinations(String[] array1, String[] array2) that prints all pairs combining a value of array1 with a value of array2.
For example, if a1 = {"red", "white", "green", "blue"} and a2 = {"shirt", "pants", "shoes"}, then print_combinations(a1, a2) should print:
red shirt
red pants
red shoes
white shirt
white pants
white shoes
green shirt
green pants
green shoes
blue shirt
blue pants
blue shoes
Practice Problem 26
Write a function multiplication_table(int k) that satisfies these specs:
- If k is less than 0, the function returns null.
- The function returns a 2-dimensional array of size (k+1)x(k+1), that contains the multiplication table of integers from 0 to k. The result, at position [i][j] contains the value i*j.
For example:
- multiplication_table(3) should return array
{{0, 0, 0, 0}, {0, 1, 2, 3}, {0, 2, 4, 6}, {0, 3, 6, 9}}
Practice Problem 27
Write a function subarray_sums(int[] array) that returns a 2-dimensional array of integers. The result should have a number of rows and a number of columns that are equal to the length of the input array. At position [i][j], the result should contain the sum of values array[i] + array[i+1] + ... + array[j]. If j < i, result[i][j] should be 0.
For example, if a1 = {7, 3, 8, 1, 2}, then subarray_sums(a1)should return array
{{7, 10, 18, 19, 21},
{0, 3, 11, 12, 14},
{0, 0, 8, 9, 11},
{0, 0, 0, 1, 3},
{0, 0, 0, 0, 2}}
CSE 1310 -
Practice Problems -
Practice Set 3