Simple Programming Problems

Simple Programming Problems

Original website:  Adrian Neumann

Whenever I’m TA for a introductory CS class where students learn some programming language, I have trouble coming up with good exercises. Problems from Project Euler and the like are usually much too difficult for beginners, especially if they don’t have a strong background in mathematics.

This page is a collection of progressively more difficult exercises that are suitable for people who just started learning. It will be extended as I come up with new exercises. Except for the GUI questions, exercises are generally algorithmic and should be solvable without learning any libraries. The difficulty of the exercises of course somewhat depends on the programming language you use. The List exercises for example are more complicated in languages like C that don’t have build-in support for lists.

I suppose they are also useful, although much easier, whenever an experienced person wants to learn a new language.

Elementary

1. Write a program that prints ‘Hello World’ to the screen.

2.Write a program that asks the user for his name and greets him with his name.

3.Modify the previous program such that only the users Alice and Bob are greeted with their names.

4.Write a program that asks the user for a number n and prints the sum of the numbers 1 ton

5.Modify the previous program such that only multiples of three or five are considered in the sum, e.g. 3, 5, 6, 9, 10, 12, 15 for n=17

6.Write a program that asks the user for a number n and gives him the possibility to choose between computing the sum and computing the product of 1,…,n.

7.Write a program that prints a multiplication table for numbers up to 12.

8.Write a program that prints all prime numbers. (Note: if your              programming language does not support arbitrary size numbers, printing all primes up to the largest number you can represent is fine too.)

9.Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether his number was too large or too small. At the end the number of tries    needed should be printed. I counts only as one try if the user inputs the same number consecutively.

10.Write a program that prints the next 20 leap years.

11.Write a program that computes.

       4⋅∑k=1106(-1)k+12k-1=4⋅(1-1/3+1/5-1/7+1/9-1/11…).

Lists, Strings

1.Write a function that returns the largest element in a list.

2.Write function that reverses a list, preferably in place.

3.Write a function that checks whether an element occurs in a list.

4.Write a function that returns the elements on odd positions in a list.

5.Write a function that computes the running total of a list.

6.Write a function that tests whether a string is a palindrome.

7.Write three functions that compute the sum of the numbers in a list: using a for-    loop, awhile-loop and recursion.

8.Write a function on_all that applies a function to every element of a list. Use it to      print the first twenty perfect squares.

9.Write a function that concatenates two lists.

10.Write a function that combines two lists by alternatingly taking elements, e. g.[a,b,c],[1,2,3]→[a,1,b,2,c,3].

11.Write a function that merges two sorted lists into a new list.

12.Write a function that computes the list of the first 100 Fibonacci numbers.

13.Write a function that takes a number and returns a list of its digits.

14.Write functions that add, subtract, and multiply two numbers in their digit-list        representation (and return a new digit list). If you’re ambitious you can                    implement Karatsuba multiplication. Try different bases. What is the best base if      you care about speed?

15.Implement the following sorting algorithms: Selection sort, Insertion sort, Merge      sort, Quick sort, Stooge Sort. Check Wikipedia for descriptions.

16.Implement binary search.

17.Write a function that takes a list of strings an prints them, one per line, in a              rectangular frame. For example the list["Hello", "World", "in", "a", "frame"]gets          printed as:

*********

* Hello *

* World *

* in    *

* a    *

* frame *

*********

18.Write function that translated a text to Pig Latin and back. English is translated        to Pig Latin by taking the first letter of every word, moving it to the end of the          word and adding ‘ay’. The quick brown fox” becomes “Hetay uickqay rownbay          oxfay”.

Intermediate

1. Write a program that outputs all possibilities to put+or-or nothing between the numbers 1,2,…,9 (in this order) such that  the result is 100. 

    For example 1 + 2 + 3 - 4 + 5 + 6 + 78 + 9 = 100.

2.Write a program that takes the duration of a year (in fractional days) for an imaginary planet as an input and produces a leap- year rule that minimizes the difference to the planet’s solar year.

3.Implement a data structure for graphs that allows modification (insertion, deletion). It should be possible to store values at edges and nodes. It might be easiest to use a dictionary of (node, edgelist) to do this.

4.Write a function that generates a DOT representation of a graph.

5.Write a program that automatically generates essays for you.

    5.1.Using a sample text, create a directed (multi-)graph where the words of a text are nodes and there is a directed edge between u and v if u is followed by v in your sample text. Multiple occurrences lead to multiple edges.

   5.2.Do a random walk on this graph: Starting from an arbitrary node choose a random successor. If no successor exists, choose another random node.

6.Write a program that automatically converts English text to Morse code and vice versa.

7.Write a program that finds the longest palindromic substring of a given string. Try to be as efficient as possible!

Advanced

1.Given two strings, write a program that efficiently finds the longest common subsequence.

2.Given an array with numbers, write a program that efficiently answers queries of the form: “Which is the nearest larger value for the number at position i?”, where distance is the difference in array indices. For example in the array[1,4,3,2,5,7], the nearest larger value for 4 is 5. After linear time preprocessing you should be able to answer queries in constant time.

3.Given two strings, write a program that outputs the shortest sequence of character insertions and deletions that turn one string into the other.

4.Write a function that multiplies two matrices together. Make it as efficient as you can and compare the performance to a polished linear algebra library for your language. You might want to read about Strassen’s algorithm and the effects CPU caches have. Try out different matrix layouts and see what happens.

5.Given a set of d-dimensional rectangular boxes, write a program that computes the volume of their union. Start with 2D and work your way up.

GUI

1.Write a program that displays a bouncing ball.

2.Write a Memory game.

3.Write a Tetris clone.

Open Ended

1.Write a program that plays Hangman as good as possible. For example you can use a large dictionary like this and select the  letter that excludes most words that are still possible solutions.Try to make the program as efficient as possible, i.e. don’t scan the whole dictionary in every turn.

2.Write a program that plays Rock, Paper, Scissors better than random against a human. Try to exploit that humans are very bad at generating random numbers.

3.Write a program that plays Battle Ship against human opponents. It takes coordinates as input and outputs whether that was a hit or not and its own shot’s coordinates.

Other Collections

Of course I’m not the first person to come up with the idea of having a list like this.

1. John Dalbey’s collection

2. Several small problems Programming Practice

3. CPE 101Projects

4. Code Kata

5. 99 Lisp Problems,99 Haskell Problems. Most of these can also be done in other       languages.

6. Rosetta Code Programming Tasks. These come with solutions in many                     languages!

7. Code Golf Challenges. The goal here is to solve the problem with as few                   characters as possible.

8. SPOJ Problems. This is a list of more than 13000 Problems!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,355评论 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,430评论 5 6
  • 10月12日茶旅日记 第一幕 ***月***日,小雨微风,休息日。 据朋友介绍,在***有一家纯正的中国茶馆。说实...
    晓壹2号阅读 317评论 0 1
  • 各位爸爸妈妈们,大家好! 因为冷空气,早起感觉骤凉,大家记得添衣保暖,感冒咳嗽真难受。 昨天放学前,王金可同学反复...
    阳光温温阅读 392评论 2 8
  • 我是一粒带着微光的尘埃 我想知道荒洪宇宙的精彩 我听见悠长地的啸声在幽幽深海 我看见灼烧烈的赤轮摔开万里云霭 我愿...
    君爔阅读 816评论 0 3