index | submit | rank | book

triple – Triple

Write a program that reads several number and prints their triple.

2 times 3 is 6

Input and Output

The input contains several lines each with one integer x where -100 000 < x < 100 000.

Each line of output should contain a corresponding integer y where y = 3x.

The number x may be given in the input with leading zeroes. The number y should be given in the output without leading zeroes. Input is terminated by the end-of-file (EOF)

Example input

2
360
123

Example output

6
1080
369

The triple function

Your program should be implemented using a triple function that receives one integer as argument and returns an integer. Please refer to the information for your chosen language:

The triple function should not print anything. It should just perform the computation and return an integer. The function and input/output processing must exist in the same program. Create a single submission with the function and main program. If you’re confused, try earning a partial score first.

Specifically for this exercise when using Python, JavaScript, Lua or Ruby, avoid using sys.exit(), process.exit(), os.exit() or exit, as your program is appended with some extra assertions in one of the test sets.

Scoring

Submit your solution to be graded according to the following list:

Hints

If you do not know where to start, read the Computer Science by Example book. First, setup your environment then learn the programming basics. Here are some hints:

  1. Automated judge: Keep in mind that when your program is submitted it will not be run by a human but instead by an automated judge. Instructions should be followed exactly or the judge will not give you a full score.

    Your program should not print messages like Please type a number: or The triple is:. Instead, just print the result followed by a line break as in the example output.

  2. Produce output as you go: You do not need to accumulate input and then produce everything at the end. It is enough to produce output as you go. As soon as you read a number write its triple to standard output.

  3. Detecting the end of file. In this exercise, input is terminated by the end-of-file (EOF). Here are ways to detect EOF in C, Python and Haskell:

    • In C. The scanf function returns the numbers of items read from stdin. Since this exercise requires you to read a number each line, you can compare scanf’s result to one as a while condition:

        while (scanf(...)==1) {
            ...
        }
      

      Which translates to, “while you’re able to read one item from standard input, do …”

    • In Python. The pattern for line in sys.stdin: can be used to create a loop where a file is processed line by line until the end-of-file (EOF).

    • In Haskell. You can use interact to declare the main function and implement your solution as a function from String to String:

        io :: String -> String
        io = ...
      
        main :: IO
        main = interact io
      

      EOF is then represented as the nil list constructor ("" or []) at the end of the argument String.

    On the terminal, you can simulate the end-of-file (EOF) by holding “Ctrl” and pressing “D”, i.e., Ctrl-D.

  4. Beware of leading zeroes. C users should beware of leading zeroes. Use %d instead of %i to avoid treating numbers with leading zeroes as octals. Test your program with 010, 011 and 012. Does it print 30, 33 and 36?

  5. Beware of the data types. Users of Python and dinamically-typed languages beware. The triple function will be tested with proper integer values as its argument and not with integers encoded in strings. For example, it will be tested as triple(360) but never as triple("360").

  6. Further information. Sections 4.4. Functions and 4.5. Operators of the Computer Science by Example book talks a bit about what is needed to solve this exercise.

try first: repeat triple1

try also: inc

try next: add

index | submit | rank | book

Copyright © 2020-2023 Rudy Matela
All rights reserved