Solved Problems
Output a string to the console
Write the string
"Hello World!" to STDOUT
ruby
puts "Hello World!"
$stdout<<"Hello World!"
java
System.out.println("Hello World!");
System.out.printf("Hello World!\n");
perl
print "Hello World!\n"
groovy
println "Hello World!"
scala
println("Hello World!")
printf("Hello World!\n")
python
print "Hello World!"
cpp
std::cout << "Hello World" << std::endl;
std::printf("Hello World\n");
Console::WriteLine(L"Hello World");
fsharp
printfn "Hello World!"
erlang
io:format("Hello, World!~n").
ocaml
print_string "Hello world!\n";;
csharp
System.Console.WriteLine("Hello World!")
php
echo 'Hello World!';
/****
* For some (security)reason I couldn't
* submit this without adding a space to
* the functionname. Please remove it :)
****/
// The correct way in command line-mode. :)
f write(STDOUT, "Hello World!\n");
* For some (security)reason I couldn't
* submit this without adding a space to
* the functionname. Please remove it :)
****/
// The correct way in command line-mode. :)
f write(STDOUT, "Hello World!\n");
haskell
main = putStrLn "Hello World!"
clojure
(println "Hello World!")
Define a string containing special characters
Define the literal string
"\#{'}${"}/"
ruby
special = '\#{\'}${"}/'
java
String special = "\\#{'}${\"}/";
perl
$special = '\#{\'}${"}/';
$special = q(\#{'}${"}/);
groovy
special = "\\#{'}\${\"}/"
special = '\\#{\'}${"}/'
special = /\#{'}${'$'}{"}\//
scala
val special = "\\#{'}${\"}/"
val special2 = """\#{'}${"}/"""
val special2 = """\#{'}${"}/"""
python
# yes, Python has way too many forms of string literals :)
print "\\#{'}${\"}/"
print "\\#{'}${"'"'"}/"
print r"""\#{'}${"}/"""
print '\\#{\'}${"}/'
print '\\#{'"'"'}${"}/'
print r'''\#{'}${"}/'''
print "\\#{'}${\"}/"
print "\\#{'}${"'"'"}/"
print r"""\#{'}${"}/"""
print '\\#{\'}${"}/'
print '\\#{'"'"'}${"}/'
print r'''\#{'}${"}/'''
cpp
std::string special = "\\#{'}${\"}/";
String^ special = L"\\#{'}${\"}/";
fsharp
let special = "\#{'}${\"}/"
erlang
Special = "\\#{'}\${\"}/",
ocaml
"\\#{'}${\"}/"
csharp
string verbatim = @"\#{'}${""""}/";
string cStyle = "\\#{'}${\"\"}/";
string cStyle = "\\#{'}${\"\"}/";
php
$special = "\\#{'}\${\"}/";
$special = '\#{\'}${"}/';
haskell
putStrLn "\"\\#{'}${\"}/\""
clojure
(def special "\\#{'}${\"}/")
Define a string containing variables and expressions
Given variables a=3 and b=4 output
"3+4=7"
ruby
puts "#{a}+#{b}=#{a+b}"
puts "#{a}+#{b}=%s" % (a + b)
java
System.out.println(a + "+" + b + "=" + (a+b));
System.out.printf("%d+%d=%d\n", a, b, a + b);
perl
print "$a+$b=${a+b}\n"
sprintf("%d+%d=%d", $a, $b, $a + $b);
print $a, '+', $b, '=', $a + $b;
groovy
println "$a+$b=${a+b}"
printf "%d+%d=%d\n", a, b, a + b
scala
printf("%d+%d=%d\n", a, b, a + b)
"%d+%d=%d".format(a, b, a + b)
python
class EvalDict(dict):
def __getitem__(s, k):
return eval(k, s)
a=3; b=4
"%(a)d+%(b)d=%(a+b)d" % EvalDict(locals())
def __getitem__(s, k):
return eval(k, s)
a=3; b=4
"%(a)d+%(b)d=%(a+b)d" % EvalDict(locals())
a=3; b=4
"%d+%d=%d" % (a, b, a+b)
"%d+%d=%d" % (a, b, a+b)
cpp
Console::WriteLine(L"{0}+{1}={2}", a, b, a+b);
std::printf("%d+%d=%d\n", a, b, a+b);
std::cout << boost::format("%|1|+%|1|=%|1|") % a % b % (a+b) << std::endl;
fsharp
let a, b = 3, 4
printfn "%d+%d=%d" a b (a+b)
printfn "%d+%d=%d" a b (a+b)
erlang
A = 3, B = 4,
io:format("~B+~B=~B~n", [A, B, (A+B)]).
io:format("~B+~B=~B~n", [A, B, (A+B)]).
ocaml
Printf.printf "%d+%d=%d" a b (a+b);;
Printf.printf "%d+%d=%d" a b (a+b);;
csharp
int a = 3;
int b = 4;
Console.WriteLine("{0}+{1}={2}", a,b,a+b);
int b = 4;
Console.WriteLine("{0}+{1}={2}", a,b,a+b);
php
echo "$a+$b=".($a+$b);
printf("%d+%d=%d\n", $a, $b, $a + $b);
haskell
import Text.Printf
main = do
let a = 3
let b = 4
printf "%d+%d=%d" a b (a + b)
main = do
let a = 3
let b = 4
printf "%d+%d=%d" a b (a + b)
clojure
(format "%d + %d = %d" a b (+ a b))
Reverse the characters in a string
Given the string
"reverse me", produce the string "em esrever"
ruby
puts "reverse me".reverse
java
String reverse = new StringBuffer("reverse me").reverse().toString();
String reverse = new StringBuilder("reverse me").reverse().toString();
String reverse = StringUtils.reverse("reverse me");
perl
$_ = reverse "reverse me"; print
groovy
reversed = "reverse me".reverse()
scala
val reversed = "reverse me".reverse.toString
python
"reverse me"[::-1]
cpp
String^ s = "reverse me";
array<Char>^ sa = s->ToCharArray();
Array::Reverse(sa);
String^ sr = gcnew String(sa);
array<Char>^ sa = s->ToCharArray();
Array::Reverse(sa);
String^ sr = gcnew String(sa);
std::string s = "reverse me";
std::reverse(s.begin(), s.end());
std::reverse(s.begin(), s.end());
std::string s = "reverse me";
std::string sr(s.rbegin(), s.rend());
std::string sr(s.rbegin(), s.rend());
std::string s = "reverse me";
std::swap_ranges(s.begin(), (s.begin() + s.size() / 2), s.rbegin());
std::swap_ranges(s.begin(), (s.begin() + s.size() / 2), s.rbegin());
fsharp
let reversed = new String (Array.rev ("reverse me".ToCharArray()))
let word = "reverse me"
//reverse the word
let reversedword =
word.ToCharArray()
|> Array.fold(fun acc x -> List.Cons(x,acc)) []
//reverse the word
let reversedword =
word.ToCharArray()
|> Array.fold(fun acc x -> List.Cons(x,acc)) []
erlang
Reversed = lists:reverse("reverse me"),
Reversed = revchars("reverse me"),
ocaml
let reverse str =
let len = String.length str in
let buf = Buffer.create len in
for i = 0 to len-1 do
Buffer.add_char buf str.[len-i-1]
done;
Buffer.contents buf;
let len = String.length str in
let buf = Buffer.create len in
for i = 0 to len-1 do
Buffer.add_char buf str.[len-i-1]
done;
Buffer.contents buf;
csharp
var str = "reverse me";
Console.WriteLine(new String(str.Reverse().ToArray()));
Console.WriteLine(new String(str.Reverse().ToArray()));
php
$reversed = strrev("reverse me");
haskell
reverse "reverse me"
clojure
(require '[clojure.contrib.str-utils2 :as str])
(str/reverse "reverse me")
(str/reverse "reverse me")
(apply str (reverse "reverse me"))
Reverse the words in a string
Given the string
"This is a end, my only friend!", produce the string "friend! only my end, the is This"
ruby
reversed = text.split.reverse.join(' ')
java
List list = new ArrayList();
StringTokenizer st = new StringTokenizer(text, " ");
while(st.hasMoreTokens()) {
list.add(0, st.nextToken());
}
StringBuffer sb = new StringBuffer();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String word = (String) iterator.next();
sb.append(word);
if (iterator.hasNext()) {
sb.append(" ");
}
}
String reversed = sb.toString();
StringTokenizer st = new StringTokenizer(text, " ");
while(st.hasMoreTokens()) {
list.add(0, st.nextToken());
}
StringBuffer sb = new StringBuffer();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String word = (String) iterator.next();
sb.append(word);
if (iterator.hasNext()) {
sb.append(" ");
}
}
String reversed = sb.toString();
List<String> ls = Arrays.asList("This is the end, my only friend!".split("\\s"));
Collections.reverse(ls);
StringBuilder sb = new StringBuilder(32); for (String s : ls) sb.append(" ").append(s);
String reversed = sb.toString().trim();
Collections.reverse(ls);
StringBuilder sb = new StringBuilder(32); for (String s : ls) sb.append(" ").append(s);
String reversed = sb.toString().trim();
String reversed = StringUtils.reverseDelimited("This is the end, my only friend!", ' ');
perl
$reversed = join ' ', reverse split / /, $text;
groovy
reversed = "This is the end, my only friend!".split().reverse().join(' ')
reversed = "This is the end, my only friend!".tokenize(' ').reverse().join(' ')
def revdelim(c, s) { StringUtils.reverseDelimited(s, c) }
revwords = this.&revdelim.curry(" " as char)
reversed = revwords("This is the end, my only friend!")
revwords = this.&revdelim.curry(" " as char)
reversed = revwords("This is the end, my only friend!")
reversed = StringUtils.reverseDelimited("This is the end, my only friend!", " " as char)
scala
"This is the end, my only friend!".split(" ").reverse.reduceLeft( (x,y) => x+' '+y )
val reversed = revwords("This is the end, my only friend!")
(("This is the end, my only friend!" split " ") reverse) mkString " "
python
' '.join(reversed("This is a end, my only friend!".split()))
cpp
array<Char>^ sep = {L' '};
array<String^>^ words =
String(L"This is the end, my only friend!").Split(sep, StringSplitOptions::RemoveEmptyEntries);
Array::Reverse(words); String^ newwords = String::Join(L" ", words);
array<String^>^ words =
String(L"This is the end, my only friend!").Split(sep, StringSplitOptions::RemoveEmptyEntries);
Array::Reverse(words); String^ newwords = String::Join(L" ", words);
std::string words = "This is the end, my only friend!"; std::vector<std::string> swv;
boost::split(swv, words, boost::is_any_of(" ")); std::reverse(swv.begin(), swv.end());
std::string newwords = (std::for_each(swv.begin(), swv.end(), StringTAndJ())).value();
boost::split(swv, words, boost::is_any_of(" ")); std::reverse(swv.begin(), swv.end());
std::string newwords = (std::for_each(swv.begin(), swv.end(), StringTAndJ())).value();
fsharp
let reversed = String.Join(" ", Array.rev("This is the end, my only friend!".Split [|' '|]))
erlang
Reversed = string:join(lists:reverse(string:tokens("This is the end, my only friend!", " ")), " "),
ocaml
let reverse str =
let len = String.length str in
let buf = Buffer.create len in
for i = 0 to len-1 do
Buffer.add_char buf str.[len-i-1]
done;
Buffer.contents buf;;
let len = String.length str in
let buf = Buffer.create len in
for i = 0 to len-1 do
Buffer.add_char buf str.[len-i-1]
done;
Buffer.contents buf;;
csharp
var str = "This is a end, my only friend!";
str = String.Join(" ", str.Split().Reverse().ToArray());
Console.WriteLine(str);
str = String.Join(" ", str.Split().Reverse().ToArray());
Console.WriteLine(str);
php
$reversed_words = implode(" ", array_reverse(explode(" ", "This is the end, my only friend!")));
haskell
unwords (reverse (words "This is the end, my only friend!"))
clojure
(require '[clojure.contrib.str-utils2 :as str])
(str/join " " (reverse (str/split "this is the end, my only friend!" #" ")))
(str/join " " (reverse (str/split "this is the end, my only friend!" #" ")))
Make a string uppercase
Transform
"Space Monkey" into "SPACE MONKEY"
ruby
uppper = text.upcase
java
String upper = text.toUpperCase();
perl
print uc "Space Monkey"
groovy
println "Space Monkey".toUpperCase()
scala
println("Space Monkey".toUpperCase)
python
"Space Monkey".upper()
cpp
String(L"Space Monkey").ToUpper();
std::string s = "Space Monkey";
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
std::string s = "Space Monkey";
boost::to_upper(s);
boost::to_upper(s);
fsharp
printfn "%s" ("Space Monkey".ToUpper())
printfn "%s" (String.uppercase "Space Monkey")
erlang
io:format("~s~n", [string:to_upper("Space Monkey")]).
ocaml
String.uppercase "Space Monkey";;
csharp
string output = "Space Monkey"
System.Console.WriteLine(output.ToUpper())
System.Console.WriteLine(output.ToUpper())
php
echo strtoupper("Space Monkey");
haskell
toUpperCase oldstring converted = if oldstring == ""
then converted
else toUpperCase (tail(oldstring)) (converted ++ [Char.toUpper(head(oldstring))])
toUpperCase "Space Monkey" ""
then converted
else toUpperCase (tail(oldstring)) (converted ++ [Char.toUpper(head(oldstring))])
toUpperCase "Space Monkey" ""
clojure
(.toUpperCase "Space Monkey")
