Programming Problems for Learners....

This page is an attempt to help you master programming, by taking small steps at a time. We start with something trivial, and work our way up to very difficult problems. Difficulty is, of course, dependent on how much you have experience. So, it is better to go ahead and attempt everything in sequence…. Along with the problems, a few questions/variants are also listed. Try them too — it should deepen your understanding.

  1. Write a program which prints "Welcome to programming".
  2. Write a program which asks you for your name ("Please enter your name:"), and then says "Welcome sss, to programming" where sss is replaced with the name you have given.
    1. In the program, was the prompt and your entry of name on separate lines? Try a single line.
    2. What was the name of the variable you used to store the name? Is there any restriction on the length of name permitted?
  3. Write a program which takes two numbers from user ("Please enter number 1", "Please enter number 2"), and prints their sum.
    1. Can we enter both numbers in one line, separated by space?
    2. Can we do with a single question?
    3. Print the numbers and their sum.
  4. Revise above, to print sum, difference, product and quotient.
    1. Make the output look clean with proper alignment.

arrays and sequences ….

  1. Create a sequence as follows in an array.
    1. Every element has value k (for a given k). e.g. 3 3 3 3 3 3 3 3
    2. Random elements within a range (say, 0 to 100, or 0 to 1)
    3. Sequence in arithmetic progression starting with a0, at interval of r
    4. Sequence in geometric progression starting with a0, at multiple of r
  2. For a sequence stored in an array A, do the following
    1. Compute the minimum value
    2. Compute the maximum value
    3. Compute the sum
    4. Count number of elements greater than x
    5. Count number of unique elements — with sorting, and without sorting
    6. Count the longest sub-sequence of ascending values
  3. For a sequence of elements with only 0 or 1 in them:
    1. Compute the fraction of 1s over the sequence
    2. Compute the number of transitions from 0 to 1, and 1 to 0
    3. Compute the number of sequences of 1s
    4. Compute the length of the longest sequence of 1s
  1. For a 2-D matrix of values, do the following
    1. produce transpose of the matrix
    2. check if the diagonal elements are all having value 1
    3. check if the values below the diagonal are all zeros
    4. check if the values above the diagomal are all zeros
  1. For a string sequence do the following:
    1. how many occurrences of a character are there in the string?
    2. how many occurrences of a substring are there in the string?
    3. Allow ? as a wildchar in the substring
    4. Allow * as an open wild char in the substring

Given a file of text (a report, a document, etc)

  1. Given a corpus of text (say a web document, or a report) find the probability of a vowel following a consonant. A consonant may be followed by a consonant, a vowel, white space (blanks, newlines, etc), or a punctuation.
  2. Write a program which reads a file containing text words (for example, this problem statement itself), ignores all punctuation marks, and reports how many words are of various lengths as follows:

length 1: 3
length 2: 24
length 3: 8

etc. "a" is of length 1, "be" is 2, "ant" is 3, and so on. There is no need to remove duplicate words.

  1. Search for a given string S in the text, and report if it occurs or not. Just yes or no.
  2. Extend the previous, report how many times it occurs, and the corresponding lines. A match will not span across multiple lines. Look for overlapping matches also.
  3. Provide for a "?" in S which will match any one character. That is, "a?t" will match ant, amt, awt, etc.

Validation programs

Here you write functions to check if a given input is valid as given in the respective problem. All functions return zero if input is valid, and some number > 0 in case of failures. The number can be used to identify the error/violation.

  1. A string consists of only alphabets
  2. A string consists of only numbers — binary, decimal, hexa?
  3. A string is a syntactically valid e-mail address
  4. A sequence of numbers is sorted.
  5. A sequence of strings is sorted.
  6. A sequence is nearly sorted.
  7. Given number is within a given range.

Specific tasks

#Produce the base-64 representation of a given character string. How easy is it to change the mapping of code to printable-characters?
#Given a pair of linear simultaneous equations, write a program to solve them. The equation ax+by=c, will be given as three floating point numbers a, b, c in one line, for input. The output will be x = …, y = …, if the equation has a unique solution. Else, print a suitable message.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License