CodeMan

Each of us must find a way to love the world. You have found yours.

0%

Two Fer

Introduction

Two-fer or 2-fer is short for two for one. One for you and one for me.

1
2
"One for X, one for me."

When X is a name or “you”.

If the given name is “Alice”, the result should be “One for Alice, one for me.” If no name is given, the result should be “One for you, one for me.”

Solve:

Bash

version 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env bash
set -o errexit
set -o pipefail

main () {
result="One for you, one for me."
if [[ ! "$1" ]] ;then
echo $result
else
echo "${result/you/$1}"
fi

}

main "$@"

version 2:

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env bash
set -o errexit
set -o pipefail

main () {
result="One for you, one for me."
echo "${result/you/${1:-you}}"
}

main "$@"

相关知识点:

模板字符串:

函数参数默认值:

Php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

function twoFer($name="you")
{
$result = 'One for {$name}, one for me.';
return $result;
}


if (empty($argv[1])) {
echo twoFer("you");
} else {
echo twoFer($argv[1]);
}

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class TwoFer {
public static void print(String msg) {
System.out.println(msg);
}

public static String twoFerString(String name) {
return String.format("One for %s, one for me.", name);
}

public static String twoFer(String name) {
String defaultName = "you";

if ((name != null) && !"".equals(name)) {
return twoFerString(name);
} else {
return twoFerString(defaultName);
}
}

public static void main(String[] args) {
if ((args != null) && (args.length > 0)) {
print(twoFer(args[0]));
} else {
print(twoFer(null));
}
}
}

Golang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
"fmt"
"os"
)

func toFerMsg(name string) string {
return fmt.Sprintf("One for %s, one for me.", name)
}

func twoFer(name string) string {
defaultName := "you"
if name == "" {
return toFerMsg(defaultName)
} else {
return toFerMsg(name)
}
}

func main() {
if len(os.Args) > 1 {
fmt.Println(twoFer(os.Args[1]))
} else {
fmt.Println(twoFer(""))
}

}


Erlang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#!/usr/local/bin/escript

-module(two_fer).

-import(io, [format/2]).
-export([twofer/0, twofer/1, main/1]).

twoferMsg(N) -> io:format("One for ~s, one for me.", [N]).

twofer(N) -> twoferMsg(N).
twofer() -> twoferMsg("you").

main(Args) ->
case Args of
"" -> twofer();
_ -> twofer(Args)
end.





1
2
3
4
5
➜ chmod +x twofer.erl
➜ two_fer.erl boo
One for boo, one for me.%
➜ two_fer.erl
One for you, one for me.%

Clojure

1
2
3
4
5
6
7
8
9
10
11
12
(ns two-fer.core
(:gen-class))

(defn twofer [name]
(str (format "One for %s, one for me." name)))

(defn -main
[& args]
(if-not (empty? args)
(println (twofer (first args)))
(println (twofer "you"))))

1
2
3
4
5
6
7
8
9
10
11
➜  two-fer lein uberjar                                                                                                 
Compiling two-fer.core
Created /Users/yangandrew/work/clojure/two-fer/target/uberjar/two-fer-0.1.0-SNAPSHOT.jar
Created /Users/yangandrew/work/clojure/two-fer/target/uberjar/two-fer-0.1.0-SNAPSHOT-standalone.jar

➜ two-fer java -jar /Users/yangandrew/work/clojure/two-fer/target/uberjar/two-fer-0.1.0-SNAPSHOT-standalone.jar foo boo
One for foo, one for me.

➜ two-fer java -jar /Users/yangandrew/work/clojure/two-fer/target/uberjar/two-fer-0.1.0-SNAPSHOT-standalone.jar
One for you, one for me.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import sys

def twofer(name):
return "One for %s, one for me." % name

def main(argv):
if len(argv) > 1:
print twofer(argv[1])
else:
print twofer("you")


if __name__ == '__main__':
main(sys.argv)


1
2
3
4
5
(venv) ➜  twofer python twofer.py 
One for you, one for me.
(venv) ➜ twofer python twofer.py Bob
One for Bob, one for me.