-
-
Save yzyzsun/8248274b9560628f6beac46c6bfca08b to your computer and use it in GitHub Desktop.
A polyglot summation challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The summation challenge, extended from the classical a-plus-b problem, is a | |
| programming challenge for beginners. A programmer is asked to write a program to | |
| read multiple integers separated by whitespace from a single line and then print | |
| out their sum. Although not difficult, different solutions to this problem can | |
| exhibit different characteristics of programming languages. Once I start to | |
| learn a new language, I will add a new solution here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main(void) { | |
| int s = 0, n; | |
| while (scanf("%d", &n) == 1) s += n; | |
| printf("%d\n", s); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (use '[clojure.string :only (split)]) | |
| (println (reduce + (map read-string (split (read-line) #" ")))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| int main() { | |
| int s = 0, n; | |
| while (std::cin >> n) s += n; | |
| std::cout << s << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| class sum { | |
| static void Main() { | |
| Console.WriteLine(Console.ReadLine().Split().Sum(x => Int32.Parse(x))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:io'; | |
| void main() { | |
| int sum = stdin.readLineSync()!.split(' ').fold(0, (s, n) => s + int.parse(n)); | |
| print(sum); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env escript | |
| main(_) -> | |
| io:format("~w~n", | |
| [lists:foldl(fun(X, Sum) -> element(1, string:to_integer(X)) + Sum end, | |
| 0, string:split(io:get_line(""), " ", all))]). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env elixir | |
| IO.gets("") | |
| |> String.split | |
| |> Stream.map(&String.to_integer/1) | |
| |> Enum.sum | |
| |> IO.puts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| open System | |
| open System.Linq | |
| Console.ReadLine().Split().Sum(fun x -> Int32.Parse(x)) | |
| |> Console.WriteLine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "strconv" | |
| ) | |
| func main() { | |
| sum := 0 | |
| scanner := bufio.NewScanner(os.Stdin) | |
| scanner.Split(bufio.ScanWords) | |
| for scanner.Scan() { | |
| if i, err := strconv.Atoi(scanner.Text()); err == nil { | |
| sum += i | |
| } | |
| } | |
| fmt.Printf("%d\n", sum) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| main :: IO () | |
| main = interact $ show . sum . map read . words |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class sum { | |
| public static void main(String[] args) { | |
| var sum = 0; | |
| var sc = new Scanner(System.in); | |
| while (sc.hasNextInt()) sum += sc.nextInt(); | |
| System.out.println(sum); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| readline() |> | |
| split |> | |
| a -> map(x -> parse(Int, x), a) |> | |
| sum |> | |
| show |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| const readline = require('readline'); | |
| const rl = readline.createInterface({ input: process.stdin }); | |
| rl.on('line', line => { | |
| const sum = line.split(' ').map(x => Number(x)).reduce((x, y) => x + y); | |
| console.log(sum); | |
| rl.close(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require :uiop) | |
| (print (loop for i in (uiop:split-string (read-line)) sum (parse-integer i))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @import Foundation; | |
| int main(void) { | |
| @autoreleasepool { | |
| NSFileHandle *stdin = [NSFileHandle fileHandleWithStandardInput]; | |
| NSFileHandle *stdout = [NSFileHandle fileHandleWithStandardOutput]; | |
| NSString *input = [[NSString alloc] initWithData:stdin.availableData encoding:NSASCIIStringEncoding]; | |
| NSArray *array = [input componentsSeparatedByString:@" "]; | |
| int sum = 0; | |
| for (NSString *number in array) { | |
| sum += number.intValue; | |
| } | |
| [stdout writeData:[[NSString stringWithFormat:@"%d\n", sum] dataUsingEncoding:NSASCIIStringEncoding]]; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ocaml | |
| read_line () | |
| |> String.split_on_char ' ' | |
| |> List.map int_of_string | |
| |> List.fold_left (+) 0 | |
| |> print_int |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| program sum; | |
| var | |
| s, n: integer; | |
| begin | |
| s := 0; | |
| while not eoln do | |
| begin | |
| read(n); | |
| s := s + n; | |
| end; | |
| writeln(s); | |
| end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| print(sum(map(int, input().split()))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cat(sum(strtoi(unlist(strsplit(readline(), " "))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env raku | |
| my $list = get.split(' ').map: *.Numeric; | |
| say $list.sum; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| puts gets.split.collect(&:to_i).inject(:+) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io; | |
| fn main() { | |
| let mut input = String::new(); | |
| io::stdin().read_line(&mut input).unwrap(); | |
| let sum: i32 = input | |
| .split_whitespace() | |
| .map(|x| x.parse::<i32>().unwrap()) | |
| .sum(); | |
| println!("{}", sum); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.io.StdIn.readLine | |
| object sum { | |
| def main(args: Array[String]): Unit = { | |
| println(readLine split ' ' map (_.toInt) reduce (_+_)) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (display (do ((n 0 (read)) (s 0 (+ s n))) ((eof-object? n) s))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| read input | |
| echo $((${input// /+})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| val input = valOf (TextIO.inputLine TextIO.stdIn) | |
| val numbers = String.tokens Char.isSpace input | |
| val sum = foldl (fn (n, s) => s + valOf (Int.fromString n)) 0 numbers | |
| in | |
| print (Int.toString sum) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env gst | |
| strings := stdin nextLine subStrings: ' '. | |
| integers := strings collect: [ :x | x asInteger ]. | |
| sum := integers inject: 0 into: [ :sum :x | sum + x ]. | |
| Transcript showCr: sum asString. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print(readLine()!.split(separator: " ").map({ Int($0)! }).reduce(0, +)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment