Programming Team:  Collected Practice Problems, 19-Jan-05
Mt Lebanon HS 2004-5
David Kosbie

Link to the Programming Team Home Page.

Question 1. Read in four integers. Print the absolute value of each number, then print the smallest absolute value, then print the difference between the largest and smallest absolute values. Print these numbers all on the same line, separating the numbers with spaces.

Question 2. Read in four integers. These are, in order, x1, y1, x2, and y2, and they are endpoints of a non-vertical, non-horizontal line segment. Print the area of the rectangle that encloses this line segment and whose sides are parallel to the x and y axes.

Question 3. Read in a 10-letter word (with no spaces), and output "YES" if all the letters in the word "dog" occur in those letters (in any order), and "NO" otherwise. Your test must be case insensitive (so "DoG" and "doG" are the same).

Question 4. Read in a whole number n, followed by n words (one per line), and output the shortest and longest words, separated by spaces.

Question 5. Read in a whole number n, and output the sum of the exponents of its prime factoring. For example, if you read in 96040, this equals (2^3)(5^1)(7^4), so the sum is 3 + 1 + 4, so you would print out 8.

Question 6. Read in a whole number n, and output the sum of the decimal digits in n. For example, if you read in 96040, this sum is 9 + 6 + 0 + 4 + 0, so you would print out 19.

Question 7. Read in an integer n and a whole number k, where 0 <= k <= 31. Note that n is a 32-bit integer, where bit 0 is the low-order (ones) bit, bit 1 is the 2's bit, bit 2 is the 4's bit, and so on. Print out bit k of number n (which will of course be a 1 or a 0).

Question 8. Read in three numbers and print out the number with the most bits set to 0 when represented in binary.

Question 9. Read in four whole numbers, a, b, c, and d. You are guaranteed that the product of some of those numbers equals the product of the rest of the numbers. So, perhaps a = b*c*d, or a*b = c*d, or a*d = b*c. Print out a line showing the correct equality. Always place a on the left-hand-side. Also, on both the left-hand and right-hand sides, list the integers in the same order as given in the input. So, if the input is 3 36 6 2, your output must be 3 * 6 * 2 = 36.

Question 10. Read in three whole numbers and output "YES" if these can be the sides of a triangle and "NO" otherwise.

Question 11. Read in a string of 0's and 1's representing a number in binary, and print out the decimal value of the number.

Question 12. Read in two integers m and n, where m is guaranteed to be exactly double n in SOME BASE (not necessarily decimal) between 2 and 10 inclusive. Print out the smallest such base. For example, if you read in 211 and 102, you should print out 3, because 211-base-3 equals 22-base-10 and 102-base-3 equals 11-base-10, and 22 is double 11.

Question 13. Read in a string containing only letters or digits and print out "YES" if it is a palindrome (same backwards as forwards) and "NO" otherwise.

Question 14. Consider this process: for Square-1, draw a square with sides of length 1, and for Square-N, connect the midpoints of Square-(N-1) to form a smaller square (so for Square-2 you connect the midpoints of Square-1). Read in a positive double A and print out the largest K such that Square-K has area larger than A.

Question 15. Goldbach's Conjecture states that every even number greater than 2 can be expressed as the sum of two prime numbers. This conjecture has never been proven or disproven, though it has been empirically confirmed to very large numbers. Read in an even number greater than 2 and print out two prime numbers which sum to that number. If there are more than one such pair, choose the pair that includes the largest prime possible. Print out the two prime numbers one-per-line and largest-to-smallest.

Question 16. Read in a positive integer k and output the kth prime number.

Question 17. Read in a double d and an integer k in the range [1,10] and print out the kth decimal digit of d. Sample input: 3.14159 2 Sample output: 4

Question 18. A mouse starts at the origin (0,0). At step 1, it goes 1 unit up. At step 2, 2 units right. At step 3, 3 units down. At step 4, 4 units left. And so on (5 up, 6 right, 7 down, 8 left, 9 up...). Read in an integer k in the range [1,10000] and output the (x,y) position (include a single space after each integer output, as in "(0 ,1 )".