A few simple programming exercises

Source: Internet
Author: User

Reprinted from http://blog.jobbole.com/53039/

Every time you become a teaching assistant in the introductory CS course (students learn programming language), they are stuck with good exercises. Project Euler and similar problems are often difficult for beginners, especially for those who do not have strong math backgrounds.

In this article, Adrian Neumann collects some exercises from beginner to advanced that are suitable for beginners who are just beginning to learn programming. When there are new exercises, Adrian will be added in time. In addition to some GUI problems, exercises are usually algorithm problems, no need to learn any library can be solved. The difficulty level of the course exercises sometimes depends on the programming language you use. For example, it would be difficult for a list to be solved with a C language that does not support lists.

Even if someone with some programming experience wants to learn a new language, though it's relatively easy for them, I think it's useful.

Primary

1. Write the program to print "Hello world" to the screen.

Console.log ("Hello World");

2. Write the program to enter the user's name and greet him with that name.

3. Modify the previous program so that only the two users, Alice and Bob, can greet them with their names.

4. Write the program to enter a number n and print out the and from 1 to N.

5. Modify the previous program so that the sum number contains only multiples of 3 or 5, such as n=17, the sum is: 3, 5, 6, 9, 10, 12, 15.

6. Write a program that requires the user to enter a number n, and the probability of the choice is to calculate the sum of 1 to n or calculate the product of 1 to N.

7. Write the program to print out the 12x12 multiplication table.

8. Write the program to print all prime numbers. (Note: If you are using a programming language that does not support any number of sizes, print out all the primes you can represent, including the maximum number)

9. Write a quiz game, the user must guess a secret number, after each guess the program will tell the user he guessed the number is too big or too small, until the correct guess, and finally print out the number of guesses. If the user guesses the same number in a row, it is counted only once.

10. Write a program to print out the next 20 leap years.

11. Write Program calculation:

List and string of lists

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

2. Write a function reversal list, preferably in situ reversal.

3. Write a function to check whether the specified element appears in the list.

4. Write a function that returns all the elements of an odd position in the list.

5. Write a function to calculate the sum of the running costs of the list (the running total).

6. Write a function to test whether a string is a palindrome.

7. Write three functions to calculate the number of the numbers in the list and: use for loop, while loop and recursive completion.

8. Write a function On_all iterate through each element in the list, and print out the 20 full squares of the start.

9. Write a function to connect two lists.

10. Write a function to alternately merge two lists, for example: [a,b,c] , [1,2,3][a,1,b,2,c,3] .

11. Write a function to merge two ordered lists.

12. Write a function to calculate a list of the first 100 Fibonacci numbers.

13. Write a function that returns a list of the numbers for the specified number.

14. Write a function to add and subtract two numbers, use a list of digits on each bit to implement and return a new list of numbers, if you are confident that you can achieve karatsuba multiplication. Try different cardinality, and if you care about speed you can compare which is the best cardinality.

15. Implement the following sorting algorithm: Select sort, insert sort, merge sort, quick sort, smelly cobbler sort (stooge sort). See Wikipedia for a specific description.

16. Achieve binary search.

17. Write a function, given a list of strings and print them as shown below, one line in a rectangular box. For example list ["Hello", "World", "in", "a", "frame") The result of printing is:

1234567 ********** Hello * * World * * in * * A * * Frame * *********

18. Write function to translate a text to pig Latin return, the rule of English translates to Pig Latin is: take out the first letter of each word, append ' ay ' and then put it to the end of the word. For example, "The quick brown fox" translates into "Hetay uickqay rownbay Oxfay".

Intermediate

1. Write the program in the,..., 9 (keep this order) can be arbitrarily placed + or-or both so that the result equals 100, output all possible methods. For example: 1 + 2 + 3–4 + 5 + 6 + 78 + 9 = 100.

2. The write procedure takes the one-year duration of an imaginary planet as input, creating a leap year rule that minimizes the difference from the solar years of the planet.

3. Implement data structure diagram, allow to modify (insert, delete), and be able to store the values of edges and nodes. It is possible to use the (node, edgelist) dictionary to represent the easiest to complete this function.

4, write the dot representation of the function generation graph (Translator Note: Dot language is a text graphics description language, it provides a simple way to describe graphics, and can be understood by human and computer programs. )。

5. Write the program automatically to you to generate the article:

(1) Using a sample text samples to create a forward (multi) graph, where the text word as a node, if the text in the back of U is v there is a forward edge between U and V, multiple occurrences of the generation of multiple edges.

(2) Do random traversal in this diagram: Select a random successor from a random node and randomly select another node if there is no successor.

6. The writing program automatically converts the English text into Morse code (the translator notes: Morse code (MORSE) is a kind of time-and-time broken signal codes, in different order to express different English letters, numbers and punctuation) or vice versa.

7. Write the program to find the longest palindrome substring of a given string, and implement it as efficiently as possible.

Senior

1. Given two strings, the write program finds the longest common substring efficiently.

2. Given an array of integers, write the program efficient query: The nearest neighbor number is slightly larger than the position I, where the distance is the absolute difference of the index group subscript. such as arrays[1,4,3,2,5,7],比4大的最近的数是5。先用线性时间做预处理,然后用常数时间做查询。

3. 给定两个字符串,使用字符插入和删除将其中的一个字符串转变成另一个,输出最短的插入和删除序列。

4. 写个函数实现两个矩阵相乘。尽可能高效的实现并使用较好的Linear algebra Library (linear algebra library, which can be seen here) for 性能比较。你也许想读一下 Strassen's algorithm and CPU cache effects, try different matrix layouts to see what happens.

5. Given a set of D-dimensional matrices, the Writing program calculates the volume of their intersection, starting from 2 dimensions.

Gui
    • Write a program that shows bouncing balls.
    • Write a memory game.
    • Write a Russian block project Tetris clone.

The last Open question
    1. Write a guessing game hangman as fun as possible. For example, you can use such a large dictionary and choose to exclude the letters that most words still have a solution. Implement as efficiently as possible, such as not scanning the entire dictionary every round.
    2. Write a program to play with humans rock scissors cloth (rock, Paper, Scissors), trying to prove that humans are very not good at generating random numbers.
    3. Write a program to play with human opponents battle ship (naval Warfare Chess, is a double play guessing game), input coordinates and output whether hit and the shooting coordinates.

Other collection

Of course, I'm not the first person to ask for the idea of collecting a list like the one above.

      • John Dalbey's collection
        • Several small problem programming practices
        • CPE 101 Project
      • Code Kata
      • Problems Lisp, Problems Haskell. Most of the problems here can be solved in other languages.
      • Rosetta Code Programming Tasks. These questions provide a multi-lingual solution!
      • Code Golf challenges. The goal here is to solve the problem with as few characters as possible.
      • Spoj problems. This is a list of more than 13,000 questions!

A few simple programming exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.