├── array_length.v ├── hello_world_text.v ├── hello_world_newline_omission.v ├── program_name.v ├── even_or_odd.v ├── environment_variables.v ├── greatest_common_divisor.v ├── file_size.v ├── ackermann_function.v ├── real_constants_and_functions.v ├── README.md ├── logical_operations.v ├── variables.v ├── fizzbuzz.v ├── system_time.v ├── command_line_arguments.v ├── integer_comparison.v ├── 100doors.v ├── guess_the_number.v ├── associative_array_iteration.v └── associative_array_creation.v /array_length.v: -------------------------------------------------------------------------------- 1 | println([1, 2, 3].len) 2 | -------------------------------------------------------------------------------- /hello_world_text.v: -------------------------------------------------------------------------------- 1 | println('Hello World!') 2 | -------------------------------------------------------------------------------- /hello_world_newline_omission.v: -------------------------------------------------------------------------------- 1 | print('Goodbye, World!') 2 | 3 | -------------------------------------------------------------------------------- /program_name.v: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | fn main() { 4 | program := os.args[0] 5 | println(program) 6 | } 7 | -------------------------------------------------------------------------------- /even_or_odd.v: -------------------------------------------------------------------------------- 1 | // where `x' is integer 2 | if x % 2 == 0 { 3 | // if even 4 | } else { 5 | // if odd 6 | } 7 | -------------------------------------------------------------------------------- /environment_variables.v: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | fn main() { 4 | home := os.getenv('HOME') 5 | println(home) 6 | } 7 | -------------------------------------------------------------------------------- /greatest_common_divisor.v: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | fn main() { 4 | res := math.gcd(4585, 1589) 5 | println(res) 6 | } 7 | -------------------------------------------------------------------------------- /file_size.v: -------------------------------------------------------------------------------- 1 | import os 2 | fn main() { 3 | println(os.file_size('text.txt')) 4 | println(os.file_size('/text.txt')) 5 | } 6 | -------------------------------------------------------------------------------- /ackermann_function.v: -------------------------------------------------------------------------------- 1 | fn ackermann(m int, n int) int { 2 | switch 0 { 3 | case m: 4 | return n + 1 5 | 6 | case n: 7 | return ackermann(m - 1, 1) 8 | } 9 | 10 | return ackermann(m - 1, ackermann(m, n - 1)) 11 | } 12 | -------------------------------------------------------------------------------- /real_constants_and_functions.v: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | // where `x' and `y' are f64 4 | math.E 5 | math.Pi 6 | math.sqrt(x) 7 | math.log(x) 8 | math.log10(x) 9 | math.exp(x) 10 | math.abs(x) 11 | math.floor(x) 12 | math.ceil(x) 13 | math.pow(x, y) 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rosetta-code-v 2 | Solutions for Rosetta Code in V 3 | 4 | http://www.rosettacode.org/wiki/Category:Programming_Tasks 5 | 6 | ## How to name file? 7 | Name of the task. Replace spaces, dashes, dots, slashes with underscore. All letters in lowercase. 8 | -------------------------------------------------------------------------------- /logical_operations.v: -------------------------------------------------------------------------------- 1 | fn print_logic(a, b bool) { 2 | print('a and b = ') 3 | println(a && b) 4 | 5 | print('a or b = ') 6 | println(a || b) 7 | 8 | print('not a = ') 9 | println(!a) 10 | } 11 | 12 | fn main() { 13 | print_logic(true, false) 14 | } 15 | -------------------------------------------------------------------------------- /variables.v: -------------------------------------------------------------------------------- 1 | // these examples, respectively, refer to integer, float, boolean, and string objects 2 | example1 := 3 3 | example2 := 3.0 4 | example3 := true 5 | mut example4 := 'hello' 6 | 7 | // not posible, example1 not mutable 8 | // example1 = 4 9 | 10 | // example4 now changed. 11 | example4 = 'goodbye' 12 | -------------------------------------------------------------------------------- /fizzbuzz.v: -------------------------------------------------------------------------------- 1 | fn fizzbuzz(n int) { 2 | for i := 0; i < n; i++ { 3 | if i % 15 == 0 { 4 | println('FizzBuzz') 5 | } else if i % 3 == 0 { 6 | println('Fizz') 7 | } else if i % 5 == 0 { 8 | println('Buzz') 9 | } else { 10 | println(i) 11 | } 12 | } 13 | } 14 | 15 | fn main() { 16 | fizzbuzz(50) 17 | } 18 | -------------------------------------------------------------------------------- /system_time.v: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | fn main() { 4 | t := time.now() 5 | 6 | // String representation 7 | println(t) 8 | 9 | // Unix time (number of seconds that have elapsed since 1970-01-01) 10 | println(t.uni) 11 | 12 | // There are different formats 13 | // YYYY-MM-DD HH:MM:SS 14 | println(t.format_ss()) 15 | } -------------------------------------------------------------------------------- /command_line_arguments.v: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | fn main() { 4 | // Loops over all command line arguments. The first element is the program name 5 | for argument in os.args { 6 | println(argument) 7 | } 8 | 9 | // This loop skips the first element (program name) 10 | for i := 1; i < os.args.len; i++ { 11 | println(os.args[i]) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /integer_comparison.v: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | fn main() { 4 | print('Enter first number: ') 5 | num1 := os.get_line().int() 6 | print('Enter second number: ') 7 | num2 := os.get_line().int() 8 | 9 | if num1 < num2 { 10 | println('$num1 is less than $num2') 11 | } 12 | else if num1 == num2 { 13 | println('$num1 equals $num2') 14 | } 15 | else if num1 > num2 { 16 | println('$num1 is greater than $num2') 17 | } 18 | } -------------------------------------------------------------------------------- /100doors.v: -------------------------------------------------------------------------------- 1 | fn main() { 2 | nr := 100 3 | mut is_open := [0; nr] 4 | for pass := 0; pass < nr; pass++ { 5 | for door := pass; door < nr; door += pass + 1 { 6 | is_open[door] = (is_open[door] + 1) % 2 7 | } 8 | } 9 | for door := 0; door < 100; door++ { 10 | C.printf('door #%i is %s.\n', door+1, if is_open[door] == 1 {'open'} else {'closed'})//_open[door] == 1 {'open'} else {'closed'}) 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /guess_the_number.v: -------------------------------------------------------------------------------- 1 | import rand 2 | import time 3 | import os 4 | 5 | fn main() { 6 | t := time.now() 7 | s := t.calc_unix() 8 | rand.seed(s) 9 | 10 | num := rand.next(10) // Random number 11 | 12 | // Game loop 13 | for { 14 | println('Please guess a number from 1-10 and press ') 15 | guess := os.get_line() 16 | if guess.int() == num { 17 | println('Well guessed!') 18 | return 19 | } else { 20 | print('Incorrect! ') 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /associative_array_iteration.v: -------------------------------------------------------------------------------- 1 | // A map (also known as an associative array) is a collection of key:value pairs 2 | // Currently in 0.1.17 only string keys are allowed in maps 3 | // The keys are case sensitive. If not found: 0 is returned. 4 | 5 | fn main() { 6 | phone_book := { 7 | 'John Doe': '11111' 8 | 'Foo Bar': '22222' 9 | } 10 | 11 | // Iterate over all key:value pairs 12 | for key, value in phone_book { 13 | println('$key: $value') 14 | } 15 | 16 | // In 0.1.17 currently is no way to iterate only over the keys or values directly, maybe this will change. 17 | } 18 | 19 | // See also associative_array_creation 20 | -------------------------------------------------------------------------------- /associative_array_creation.v: -------------------------------------------------------------------------------- 1 | // A map (also known as an associative array) is a collection of key:value pairs 2 | // Currently in 0.1.17 only string keys are allowed in maps 3 | // The keys are case sensitive. If not found: 0 is returned. 4 | 5 | // There are two different ways to create a map: 6 | fn main() { 7 | 8 | // 1 9 | mut phone_book := map[string]int // int no longer requires {} 10 | phone_book['John Doe'] = 11111 11 | 12 | println(phone_book['John Doe']) // prints 11111 13 | 14 | // 2 15 | another_phone_book := { 16 | 'Foo Bar': 22222, // , is optional 17 | 'Foo Bar2': 33333, 18 | 'Foo Bar3': 44444 19 | } 20 | 21 | println(another_phone_book['Foo Bar2']) // prints 33333 22 | } 23 | 24 | // See also associative_array_iteration 25 | --------------------------------------------------------------------------------