index | submit | rank | book

swap – Swap

Write a program that reads a series of pairs of integers and strings and swaps them.

Swapping 123 and abcd

Input will consist of several lines each containing a number n and a string s separated by a single space where:

0 ≤ n ≤ 1000

|s| ≤ 30, i.e.: the string s is no more than 30 characters

For each line of input, you program should produce a line of output with the string s followed by number n.

The number n may be given with leading zeroes on the input and should be given without leading zeroes on the output.

Input is terminated by the end-of-file (EOF).

Example input

123 abcdef
64 bits

Example output

abcdef 123
bits 64

Scoring

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 string: or Please type a number:. Instead, just print the swapped pair 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-string pair, write the corresponding string-number pair 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 and a string each line, you can compare scanf’s result to two as a while condition:

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

      Which translates to, “while you’re able to read 2 items 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. Python and Haskell users should convert the number n to an integer variable to automatically get rid of leading zeroes.

  5. There is no need to check boundaries: It is out of scope of this exercise to check boundaries. The boundaries for n are given for information only: 0 ≤ n ≤ 1000. This just means that to get a full score, it is enough to write a program that works under these conditions, e.g.: a “C-int” field will be enough to hold the n value. There’s no need to check that the number n is in this interval.

try first: repeat hi swap1

try also: age

try next: owes triple inc

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/swap