Week 2: Exercises

1. Longest String

Design and perform an experiment to determine the size of the longest string that C# will allow you to create.

2. S Begins With T

Write a program that reads two strings S and T, each on its own line. The program should print "true" if S starts with T, "false" otherwise. Do not use any library functions.

3. Printing with Commas

Write a program that reads a single line containing an integer which may be arbitrarily large, and writes the integer with embedded commas.

Input:

28470562348756298345

Output:

28,470,562,348,756,298,345

4. Sorting Two Values

Write a program that reads a sequence of integers on a single line. The sequence is guaranteed to contain at most two distinct values. For example, it might be

  17 17 17 11 17 11 11 11 17 11 17 11 11

The program should print out the sequence in sorted order:

  11 11 11 11 11 11 11 17 17 17 17 17 17 

How efficient is your program?

5. Rotate a Matrix

Write a program that reads an N x N matrix of integers and prints it out rotated 90 degrees to the right.

Input:

2 4 6 8
8 6 4 2
1 3 7 9
9 7 3 1

Output:

9 1 8 2
7 3 6 4
3 7 4 6
1 9 2 8

6. Zombies

Some zombies are chasing a player. Here is a top-down view:

Image credit: © Copyright 2014-2020, Juan Linietsky, Ariel Manzur and the Godot community (CC-BY 3.0)

Each zombie can see infinitely far in a 180° field of view as indicated by the light blue half-circular shapes above. In the picture above, zombie A can see the player, but zombie B cannot.

Write a program that reads, on three lines:

Each position or vector is a pair of floating-point numbers. The program should print "can see" if the zombie can see the player, otherwise "cannot see".

7. Integer Square Root

Write a program that reads an integer n, and computes and prints the integer square root of n, i.e. the non-negative integer i such that i2 = n. If no such integer exists, print "not a square". Do not call any library functions. Your method must run in time O(log N) in the worst case.