├── 10_floyd_warshall_algo ├── floyd-warshall.py └── readme.md ├── 1_hello_world ├── hello_world.c ├── hello_world.cpp ├── hello_world.cr ├── hello_world.cs ├── hello_world.go ├── hello_world.java ├── hello_world.js ├── hello_world.kt ├── hello_world.nim ├── hello_world.php ├── hello_world.pl ├── hello_world.py ├── hello_world.rb ├── hello_world.rs ├── hello_world.ts └── readme.md ├── 2_fibonacci ├── fibonacci-recursive.js ├── fibonacci.c ├── fibonacci.cpp ├── fibonacci.cr ├── fibonacci.erl ├── fibonacci.exs ├── fibonacci.go ├── fibonacci.java ├── fibonacci.js ├── fibonacci.kt ├── fibonacci.php ├── fibonacci.pl ├── fibonacci.py ├── fibonacci.rb ├── fibonacci.rs └── readme.md ├── 3_factorialize_num ├── factorialize.c ├── factorialize.cpp ├── factorialize.cs ├── factorialize.go ├── factorialize.java ├── factorialize.js ├── factorialize.kt ├── factorialize.pl ├── factorialize.py ├── factorialize.rs └── readme.md ├── 4_dichotomic_search ├── dichotomic_search.cpp ├── dichotomic_search.java ├── dichotomic_search.js ├── dichotomic_search.py ├── dichotomic_search.rb ├── dichotomic_search.rs └── readme.md ├── 5_stone_game ├── readme.md ├── stone_game.go ├── stone_game.java ├── stone_game.js ├── stone_game.py └── stone_game.rs ├── 6_write_txt ├── data.txt ├── readme.md ├── write_c.txt ├── write_go.txt ├── write_pl.txt ├── write_py.txt ├── write_txt.c ├── write_txt.go ├── write_txt.php ├── write_txt.pl ├── write_txt.py └── write_txt.rs ├── 7_first_hashes ├── first_hashes.go ├── first_hashes.java ├── first_hashes.js ├── first_hashes.py └── readme.md ├── 8_caesar_cipher ├── caesar_cipher.c ├── caesar_cipher.py └── caesar_cipher.rb ├── 9_quick_sort └── quick_sort.php └── README.md /10_floyd_warshall_algo/floyd-warshall.py: -------------------------------------------------------------------------------- 1 | # Author: Parth Doshi 2 | # Github ID: arsenal-2004 3 | 4 | # This program is a Python 3.5 implementation of the Floyd-Warshall Shortest Path algorithm. 5 | 6 | # Defining a value to be used as the distance between nodes without any direct edges 7 | INF = 99999 8 | 9 | # Assuming graph to be a square matrix 10 | # In Python, that is represented as a list of lists. 11 | def shortestPath(graph): 12 | 13 | # The number of vertices in the graph 14 | num_v = len(graph) 15 | 16 | # Making a copy of the graph to be used as the result variable 17 | dist = graph[:] 18 | 19 | for i in range(num_v): 20 | # Selecting a source vertex 21 | for j in range(num_v): 22 | # Selecting a destination vertex 23 | for k in range(num_v): 24 | dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]) 25 | 26 | return dist 27 | 28 | 29 | def printSolution(dist): 30 | for i in range(len(dist)): 31 | for j in range(len(dist)): 32 | print("{0:7d}".format(dist[i][j]), end="") 33 | print() 34 | 35 | # Driver program to test the above program 36 | # Let us create the following weighted graph 37 | """ 38 | 10 39 | (0)------->(3) 40 | | /|\ 41 | 5 | | 42 | | | 1 43 | \|/ | 44 | (1)------->(2) 45 | 3 """ 46 | 47 | graph = [[0,5,INF,10], 48 | [INF,0,3,INF], 49 | [INF, INF, 0, 1], 50 | [INF, INF, INF, 0] 51 | ] 52 | # Print the solution 53 | dist = shortestPath(graph) 54 | printSolution(dist) 55 | -------------------------------------------------------------------------------- /10_floyd_warshall_algo/readme.md: -------------------------------------------------------------------------------- 1 | Floyd-Warshall Shortest Path Algorithm 2 | 3 | The Problem 4 | 5 | Find the shortest path between all pairs of source vertices and destination vertices in a graph using the Floyd-Warshall Shortest Path Algorithm. 6 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.c: -------------------------------------------------------------------------------- 1 | /* Hello world implementation for Rosetta Stone. */ 2 | 3 | #include 4 | 5 | void hello(char* word) 6 | { 7 | printf("Hello, World.\n"); 8 | printf("%s\n", word); 9 | } 10 | 11 | int main(void) 12 | { 13 | char* word = "Hello, World."; 14 | hello(word); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.cpp: -------------------------------------------------------------------------------- 1 | /* Hello world implementation for Rosetta Stone in C++. */ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | string hw = "Hello World"; 9 | cout<<"Hello World. "<) { 2 | greeting("Hello World") 3 | } 4 | 5 | fun greeting(str: String): Unit { 6 | print("Hello, World.") 7 | println(str) 8 | } -------------------------------------------------------------------------------- /1_hello_world/hello_world.nim: -------------------------------------------------------------------------------- 1 | # Hello World program in NIM language 2 | # Author: Hardik Kapoor 3 | 4 | echo("hello world") 5 | 6 | var x = "Hello World" 7 | echo x 8 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.php: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | print "Hello, World.\n"; 7 | my $message = "Hello World"; 8 | print "$message\n"; 9 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | # Python implementation of Hello World 3 | # Author: Eric Alcaide 4 | 5 | def main(word): 6 | # This line of code prints the first line of output 7 | print("Hello, World.") 8 | # Second line of output - the contents of 'word' here. 9 | print(word) 10 | 11 | if __name__ == "__main__": 12 | main("Hello World") -------------------------------------------------------------------------------- /1_hello_world/hello_world.rb: -------------------------------------------------------------------------------- 1 | def hello msg 2 | puts "Hello, World. #{msg}" 3 | end 4 | 5 | hello "Hello World" 6 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | print!("Hello, World."); 3 | let hello_world = "Hello World"; 4 | print!(" {}\n", hello_world); 5 | } 6 | -------------------------------------------------------------------------------- /1_hello_world/hello_world.ts: -------------------------------------------------------------------------------- 1 | function printMessage(message: string) { 2 | // This line of code prints the first line of output 3 | console.log("Hello, World."); 4 | 5 | // Write the second line of output that prints the contents of 'message' here. 6 | console.log(message); 7 | } 8 | 9 | printMessage("Hello World"); -------------------------------------------------------------------------------- /1_hello_world/readme.md: -------------------------------------------------------------------------------- 1 | # Hello World 2 | 3 | ### The Problem 4 | 5 | Print "Hello, World." on screen + declare a variable containing "Hello World" and print it on screen. 6 | 7 | ### Desired Output / Test Cases 8 | 9 | Hello, World. Hello World 10 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci-recursive.js: -------------------------------------------------------------------------------- 1 | function fibonacci(n) { 2 | if (n === 0) {return 0;} 3 | if (n === 1) {return 1;} 4 | 5 | return fibonacci(n - 1) + fibonacci(n - 2); 6 | } 7 | 8 | for (let i = 2; i < 12; i++) { 9 | console.log(fibonacci(i)); 10 | } 11 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.c: -------------------------------------------------------------------------------- 1 | /* Fibonacci implementation for Rosetta Stone project */ 2 | 3 | #include 4 | 5 | void fibonacci(void) 6 | { 7 | int prev = 0; 8 | int cur = 1; 9 | int n_cur; 10 | 11 | for (int i = 0; i < 10; i++) { 12 | n_cur = cur + prev; 13 | prev = cur; 14 | cur = n_cur; 15 | 16 | printf("%d ", cur); 17 | } 18 | printf("\n"); 19 | } 20 | 21 | int main(void) 22 | { 23 | fibonacci(); 24 | } 25 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.cpp: -------------------------------------------------------------------------------- 1 | /* Fibonacci implementation for Rosetta Stone project */ 2 | 3 | #include 4 | using namespace std; 5 | 6 | void fibonacci(void) 7 | { 8 | int prev = 0; 9 | int cur = 1; 10 | int n_cur; 11 | 12 | for (int i = 0; i < 10; i++) { 13 | n_cur = cur + prev; 14 | prev = cur; 15 | cur = n_cur; 16 | 17 | cout< 8 | fibonacci(10, 1, 1). 9 | 10 | fibonacci(0, _, _) -> 11 | ok; 12 | fibonacci(Iterations, Prev, Curr) -> 13 | io:format("~w ", [Curr]), 14 | fibonacci(Iterations - 1, Curr, Prev + Curr). 15 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.exs: -------------------------------------------------------------------------------- 1 | defmodule Fibonacci do 2 | def fib(one,two,n) do 3 | IO.puts two 4 | if n < 10, do: fib(two,one+two, n+1) 5 | end 6 | end 7 | 8 | Fibonacci.fib(1,1,1) 9 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.go: -------------------------------------------------------------------------------- 1 | // Go implementation of the Fibonacci Series 2 | // Author: Eric Alcaide 3 | 4 | package main 5 | 6 | import "fmt" 7 | 8 | func fibonacci(){ 9 | prev, actual := 0,1 10 | 11 | for q:=0; q < 10; q++{ 12 | actual, prev = prev+actual, actual 13 | fmt.Println(actual) 14 | } 15 | } 16 | 17 | func main(){ 18 | fibonacci() 19 | } -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.java: -------------------------------------------------------------------------------- 1 | public class Fibonacci { 2 | public static void main(String... args) { 3 | fibonacci(); 4 | } 5 | 6 | private static void fibonacci(){ 7 | int prev = 0; 8 | int actual = 1; 9 | int aux = 0; 10 | 11 | for (int q = 0; q < 10; ++q) { 12 | aux = actual; 13 | actual = prev + aux; 14 | prev = aux; 15 | System.out.println(actual); 16 | } 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.js: -------------------------------------------------------------------------------- 1 | function fibonacci(){ 2 | let prev = 0; let actual = 1; 3 | let aux = 0; 4 | 5 | for (let q=0; q<10;q++){ 6 | aux = actual 7 | actual = prev+aux 8 | prev = aux 9 | console.log(actual) 10 | } 11 | } 12 | 13 | fibonacci(); -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.kt: -------------------------------------------------------------------------------- 1 | fun main(args: Array) { 2 | fibonacci(); 3 | } 4 | 5 | fun fibonacci(): Unit { 6 | var prev = 0 7 | var actual = 1 8 | var aux: Int 9 | 10 | for (q in 0 until 10) { 11 | aux = actual 12 | actual = prev + aux 13 | prev = aux 14 | println(actual) 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.php: -------------------------------------------------------------------------------- 1 | 0) { 12 | [$previous, $current] = [$current, $previous + $current]; 13 | } 14 | 15 | return $current; 16 | } 17 | 18 | function fibonacci(){ 19 | print join(" ", array_map("fib_helper", range(0, 10))); 20 | } 21 | 22 | fibonacci(); 23 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | my @fibs = (1,2); 7 | 8 | while (scalar @fibs < 10) { 9 | push @fibs, $fibs[-1] + $fibs[-2]; 10 | } 11 | 12 | print "@fibs\n"; 13 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | # Python implementation of the Fibonacci Series 3 | # Author: Eric Alcaide 4 | 5 | def fibonacci(): 6 | prev, actual = 0, 1 7 | 8 | for q in range(10): 9 | actual, prev = actual+prev, actual 10 | print(actual) 11 | 12 | def main(): 13 | fibonacci() 14 | 15 | if __name__ == "__main__": 16 | main() 17 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.rb: -------------------------------------------------------------------------------- 1 | def fib i 2 | a = 0 3 | b = 1 4 | i.times do 5 | b,a = b+a, b 6 | print b, "\n" 7 | end 8 | end 9 | 10 | fib 10 11 | -------------------------------------------------------------------------------- /2_fibonacci/fibonacci.rs: -------------------------------------------------------------------------------- 1 | /* Rust implementation. Prints 10 Fibonacci Series numbers */ 2 | 3 | fn fibonacci(n: i32) -> i32 { 4 | if n <= 1 { return n;} 5 | fibonacci(n-1) + fibonacci(n-2) 6 | } 7 | 8 | fn main() { 9 | // it start to 2 and ends to 11, because of the shift of lefting behind the first 1 in the series... 10 | for i in 2..12 { 11 | print!("{} ", fibonacci(i)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /2_fibonacci/readme.md: -------------------------------------------------------------------------------- 1 | # Fibonacci Series 2 | 3 | ### The Problem 4 | 5 | Print the 10 first numbers of the Fibonacci Series starting @ the second 1. 6 | 7 | ### Desired Output / Test Cases 8 | 9 | 1 2 3 5 8 13 21 34 55 89 -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int factorialize(int n) { 6 | int result = 1; 7 | 8 | for(int i = 2; i <= n; i++) { 9 | result *= i; 10 | } 11 | return result; 12 | } 13 | 14 | int main() { 15 | assert(factorialize(0) == 1); 16 | assert(factorialize(5) == 120); 17 | assert(factorialize(7) == 5040); 18 | assert(factorialize(9) == 362880); 19 | } 20 | -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.cpp: -------------------------------------------------------------------------------- 1 | 



//Factorial in C++ using assertion 2 | //Author: Vidhant Matta 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int factorialize(int n) 9 | { 10 | unsigned long long factorial = 1; 11 | 12 | 13 | 14 | 15 | for(int i = 1; i <=n; ++i) 16 | { 17 | factorial *= i; 18 | } 19 | 20 | return factorial; 21 | } 22 | 23 | int main(){ 24 | assert(factorialize(0) == 1); 25 | assert(factorialize(5) == 120); 26 | assert(factorialize(7) == 5040); 27 | assert(factorialize(9) == 362880); 28 | } -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; // For Assert() function 3 | 4 | namespace Rosseta_Project 5 | { 6 | class Factorialize 7 | { 8 | static void Main(string[] args) 9 | { 10 | Debug.Assert( factorialize(0) == 1); 11 | Debug.Assert( factorialize(5) == 120); 12 | Debug.Assert( factorialize(7) == 5040); 13 | Debug.Assert( factorialize(9) == 362880); 14 | Debug.Assert( factorialize(10) == 3628800); 15 | Debug.Assert( factorialize(15) == 1307674368000); 16 | } 17 | 18 | // Using Int64 for big numbers. 19 | static Int64 factorialize(Int64 n) 20 | { 21 | Int64 result = 1; 22 | for (var i = n; i > 1; i--) { result *= i; } 23 | return result; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func factorialize(num int) int{ 6 | if num <=1{ 7 | return 1 8 | }else{ 9 | return num*factorialize(num-1) 10 | } 11 | } 12 | 13 | func main() { 14 | test := 5 15 | fmt.Println(factorialize(test)) 16 | } -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.java: -------------------------------------------------------------------------------- 1 | public class factorialize { 2 | public static void main(String... args) { 3 | int res = factorializeCal(6); 4 | System.out.println(res); 5 | } 6 | 7 | public static int factorializeCal(int n) { 8 | return n <= 1 ? 1 : n * factorializeCal(n-1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.js: -------------------------------------------------------------------------------- 1 | function factorialize(num) { 2 | if(num === 0) { 3 | return 1; 4 | } else if (num <= 1 ) { 5 | return num; 6 | } else { 7 | return num * factorialize(num - 1); 8 | } 9 | } 10 | 11 | factorialize(0); 12 | -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.kt: -------------------------------------------------------------------------------- 1 | fun main(args : Array) { 2 | println(factorialize(7)) 3 | } 4 | 5 | private fun factorialize(n: Int): Int { 6 | return when { 7 | n == 0 -> 1 8 | n <= 1 -> n 9 | else -> n * factorialize(n - 1) 10 | } 11 | } -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | sub factorialize { 7 | my ($num) = @_; 8 | if ($num == 0) { 9 | return 1; 10 | } else { 11 | return $num * factorialize($num - 1); 12 | } 13 | } 14 | 15 | factorialize(0) == 1 || die "Bad return value for 1\n"; 16 | factorialize(5) == 120 || die "Bad return value for 5\n"; 17 | factorialize(7) == 5040 || die "Bad return value for 7\n"; 18 | factorialize(9) == 362880 || die "Bad return value for 9\n"; 19 | 20 | print "all tests passed\n"; 21 | 22 | -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | # Factorialize code in Python for the Rosetta Stone Project 3 | # Author: Eric Alcaide 4 | 5 | def factorialize(num): 6 | return 1 if num<=1 else num*factorialize(num-1) 7 | 8 | assert factorialize(5) == 120 9 | assert factorialize(7) == 5040 10 | assert factorialize(9) == 362880 11 | 12 | print(factorialize(4)) -------------------------------------------------------------------------------- /3_factorialize_num/factorialize.rs: -------------------------------------------------------------------------------- 1 | 2 | fn factorialize(num: u32) -> u32 { 3 | match num { 4 | 0 => 1, 5 | _ => num * factorialize(num - 1) 6 | } 7 | } 8 | 9 | fn main() { 10 | let test = 5; 11 | println!("{}", factorialize(test)); 12 | } 13 | -------------------------------------------------------------------------------- /3_factorialize_num/readme.md: -------------------------------------------------------------------------------- 1 | # Factorialize 2 | 3 | ### The Problem 4 | 5 | Find the factorial of any given integer. 6 | Bonus points if you can add some assert statement. 7 | 8 | ### Input >> Desired Output / Test Cases 9 | 10 | 0: 1 11 | 12 | 5: 120 13 | 14 | 7: 5040 15 | 16 | 9: 362880 -------------------------------------------------------------------------------- /4_dichotomic_search/dichotomic_search.cpp: -------------------------------------------------------------------------------- 1 | // dichotomic search implemented in C++ for Rosetta Project 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | template 9 | bool dichotomic_search(const vector &arr, T key) { 10 | int mid = floor(arr.size() / 2); 11 | 12 | if (arr.size() < 1) { 13 | return false; 14 | } 15 | if (arr.size() == 1) { 16 | return (arr[0] == key) ? true : false; 17 | } 18 | if (arr[mid] > key) { 19 | vector arr_half1(arr.begin(), (arr.begin() + arr.size() / 2)); 20 | return dichotomic_search(arr_half1, key); 21 | } 22 | vector arr_half2((arr.begin() + arr.size() / 2), arr.end()); 23 | return dichotomic_search(arr_half2, key); 24 | } 25 | 26 | int main() { 27 | int arr_test1[5] = { 1, 2, 3, 4, 5 }; 28 | char arr_test2[5] = { 'a', 'b', 'c', 'd', 'e' }; 29 | vector test1(arr_test1, arr_test1 + sizeof arr_test1 / sizeof arr_test1[0]); 30 | vector test2(arr_test2, arr_test2 + sizeof arr_test2 / sizeof arr_test2[0]); 31 | 32 | cout << "test: try to find 5 in list { 1, 2, 3, 4, 5 }. . ."; 33 | assert(dichotomic_search(test1, 5) == true); 34 | cout << "passed\n"; 35 | cout << "test: try to find 7 in list { 1, 2, 3, 4, 5 }. . ."; 36 | assert(dichotomic_search(test1, 7) == false); 37 | cout << "passed\n"; 38 | cout << "test: try to find -1 in list { 1, 2, 3, 4, 5 }. . ."; 39 | assert(dichotomic_search(test1, -1) == false); 40 | cout << "passed\n"; 41 | cout << "test: try to find b in list { a, b, c, d, e }. . ."; 42 | assert(dichotomic_search(test2, 'b') == true); 43 | cout << "passed\n"; 44 | cout << "test: try to find c in list { a, b, c, d, e }. . ."; 45 | assert(dichotomic_search(test2, 'c') == true); 46 | cout << "passed\n"; 47 | cout << "test: try to find z in list { a, b, c, d, e }. . ."; 48 | assert(dichotomic_search(test2, 'z') == false); 49 | cout << "passed\n"; 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /4_dichotomic_search/dichotomic_search.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class dichotomic_search { 4 | public static void main(String... args) { 5 | String[] array = {"a", "b", "c"}; 6 | String v = "c"; 7 | boolean res = dichotomicSearch(v, array); 8 | System.out.println(res); 9 | 10 | } 11 | public static boolean dichotomicSearch(String v, String[] array) { 12 | int med = array.length/2; 13 | if(array.length <= 1){ 14 | return array[0] == v ? true: false; 15 | }else{ 16 | return array[med].compareTo(v) == 1 ? dichotomicSearch(v, Arrays.copyOfRange(array, 0, med)):dichotomicSearch(v, Arrays.copyOfRange(array, med, array.length)); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /4_dichotomic_search/dichotomic_search.js: -------------------------------------------------------------------------------- 1 | function dichotomic_search(array = [],v = 0) { 2 | 3 | let med = Math.round(array.length / 2); 4 | 5 | return array.length > 1 ? 6 | array[med] > v ? 7 | dichotomic_search(array.splice(0,med),v) : 8 | dichotomic_search(array.splice(med,array.length),v) : 9 | 10 | array[0] == v ? v : null; 11 | } 12 | 13 | let result = dicho_search([1,0,1,2,3], 2); 14 | console.log(result); -------------------------------------------------------------------------------- /4_dichotomic_search/dichotomic_search.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | # Python implementation of Dichotomic Search algo 3 | # Author: Eric Alcaide 4 | 5 | def dicho(arr = [], v=0): 6 | med = round(len(arr)/2) 7 | if len(arr) <= 1: 8 | return True if arr[0] == v else False 9 | else: 10 | return dicho(arr[:med], v) if arr[med] > v else dicho(arr[med:], v) 11 | 12 | 13 | result = dicho(['a', 'b', 'c'], 'c') 14 | print(result) 15 | -------------------------------------------------------------------------------- /4_dichotomic_search/dichotomic_search.rb: -------------------------------------------------------------------------------- 1 | def assert_equal(actual, expect) 2 | raise 'Assertion failed!' unless actual == expect 3 | end 4 | 5 | def dicho_search(arr, val) 6 | mid = arr.size / 2 7 | if arr.size > 1 8 | return dicho_search(arr[0...mid], val) if arr[mid] > val 9 | return dicho_search(arr[mid..-1], val) 10 | end 11 | return val if arr[0] == val 12 | nil 13 | end 14 | 15 | assert_equal(dicho_search(%w[a b d e g h j l m q x], 'z'), nil) 16 | assert_equal(dicho_search(%w[a b d e g h j l m q x], 'e'), 'e') 17 | assert_equal(dicho_search([1, 2, 3, 5, 6, 10, 15], 20), nil) 18 | assert_equal(dicho_search([1, 2, 3, 5, 6, 10, 15], 4), nil) 19 | assert_equal(dicho_search([1, 2, 3, 5, 6, 10, 15], 3), 3) 20 | -------------------------------------------------------------------------------- /4_dichotomic_search/dichotomic_search.rs: -------------------------------------------------------------------------------- 1 | /* Rust implementation for dichotomic search algorithm*/ 2 | 3 | fn dicho_search(v: Vec, val: T) -> Option { 4 | let (med, _) = v.len().overflowing_div(2); 5 | if v.len() > 1 { 6 | if v[med] > val { return dicho_search(v[0 .. med].to_vec(), val); } 7 | return dicho_search(v[med .. v.len()].to_vec(), val); 8 | } 9 | if v[med] == val { return Some(val); } 10 | None 11 | } 12 | 13 | fn main() { 14 | // Simple test 15 | let result = dicho_search([1,0,1,2,3].to_vec(), 2); 16 | println!("{}", result.unwrap()); 17 | // Running test suite: 18 | assert_eq!(dicho_search(['a','b','d','e','g','h','j','l','m','q','x'].to_vec(), 'z'), None); 19 | assert_eq!(dicho_search(['a','b','d','e','g','h','j','l','m','q','x'].to_vec(), 'e'), Some('e')); 20 | assert_eq!(dicho_search([1,2,3,5,6,10,15].to_vec(), 20), None); 21 | assert_eq!(dicho_search([1,2,3,5,6,10,15].to_vec(), 4), None); 22 | assert_eq!(dicho_search([1,2,3,5,6,10,15].to_vec(), 3), Some(3)); 23 | // If it does not panic at run-time the test passed successfully. 24 | } 25 | -------------------------------------------------------------------------------- /4_dichotomic_search/readme.md: -------------------------------------------------------------------------------- 1 | # Dichotomic Search 2 | 3 | ### The Problem 4 | 5 | Find a value inside an ordered array. 6 | 7 | ### Input >> Desired Output / Test Cases 8 | 9 | ['a','b','d','e','g','h','j','l','m','q','x'] 'z' >> null 10 | 11 | ['a','b','d','e','g','h','j','l','m','q','x'] 'e' >> 'e' 12 | 13 | [1,2,3,5,6,10,15] 20 >> null 14 | 15 | [1,2,3,5,6,10,15] 4 >> null 16 | 17 | [1,2,3,5,6,10,15] 3 >> 3 -------------------------------------------------------------------------------- /5_stone_game/readme.md: -------------------------------------------------------------------------------- 1 | # Stone Game 2 | 3 | ### The Problem 4 | 5 | Two players (numbered 1 and 2) are playing a game with ```n``` stones. Player 1 always plays first, and the two players move in alternating turns. The game's rules are as follows: 6 | 7 | * In a single move, a player can remove either 2, 3, or 5 stones from the game board. 8 | 9 | * If a player is unable to make a move, that player loses the game. 10 | 11 | Given the number of stones, find and print the name of the winner (i.e., or ) on a new line. Each player plays optimally, meaning they will not make a move that causes them to lose the game if some better, winning move exists. 12 | 13 | #### Aditional Info 14 | 15 | If ```n = 10``` , **P1** can remove either 2 or 3 stones to win the game, so we print ```First``` on a new line. Recall that each player moves optimally, so **P1** will not remove 5 stones because doing so would cause **P1** to lose the game. 16 | 17 | ### Desired Output / Test Cases 18 | 19 | * **Input:** List containing test cases: [1,2,3,4,5,6,7,10] 20 | * **Output:** 21 | 22 | 1: Second 23 | 2: First 24 | 3: First 25 | 4: First 26 | 5: First 27 | 6: First 28 | 7: Second 29 | 10: First 30 | 31 | ### Theory 32 | The goal of this code snippet is to provide an easy implementation of the Minimax algorithm, widely used in games AIs. This algorithm is composed by 2 players that evaluate each node and select the best action possible for themselves. 33 | 34 | For `n % 7` in `[0, 1]`, the first player loses, otherwise the first player wins. 35 | Please find this [link](https://theory.stanford.edu/~trevisan/cs103-14/hw2sol.pdf) for a rigorous proof. 36 | -------------------------------------------------------------------------------- /5_stone_game/stone_game.go: -------------------------------------------------------------------------------- 1 | // Code written for the Stone Game problem from Hackerrank 2 | // Go version 3 | package main 4 | 5 | import "fmt" 6 | 7 | func p2 (c int, moves [3]int) int{ 8 | min_result := 1; 9 | for i:=0; i= 0{ 11 | result := p1(c-moves[i], moves) 12 | if result < min_result{ 13 | min_result = result; 14 | } 15 | } 16 | } 17 | return min_result 18 | } 19 | 20 | func p1(c int, moves [3]int) int{ 21 | max_result := -1; 22 | for i:=0; i= 0{ 24 | result := p2(c-moves[i], moves) 25 | if result > max_result{ 26 | max_result = result; 27 | } 28 | } 29 | } 30 | return max_result 31 | } 32 | 33 | func main() { 34 | var cases = [8]int{1,2,3,4,5,6,7,10} 35 | var moves = [3]int{2,3,5}; 36 | for i:=0; i= 0){ 13 | int result = p1(c-m, moves); 14 | if(result < minResult) 15 | minResult = result; 16 | } 17 | } 18 | return minResult; 19 | } 20 | 21 | private static int p1(int c, int[] moves){ 22 | int maxResult = 0; 23 | for(int m: moves){ 24 | if(c - m >= 0){ 25 | int result = p2(c - m, moves); 26 | if(result > maxResult) 27 | maxResult = result; 28 | } 29 | } 30 | return maxResult; 31 | } 32 | 33 | private static void game(int[] cases, String[] convert, int[] moves){ 34 | for(int c: cases){ 35 | int outcome = p1(c, moves); 36 | System.out.println(convert[outcome]); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /5_stone_game/stone_game.js: -------------------------------------------------------------------------------- 1 | // Code written for the Stone Game problem from Hackerrank 2 | // JavaScript version 3 | 4 | let cases = [1,2,3,4,5,6,7,10] 5 | let moves = [2,3,5] 6 | 7 | function p2(c, moves){ 8 | let min_result = 1; 9 | for(let i = 0; i= 0){ 11 | let result = p1(c-moves[i], moves) 12 | if(result < min_result){ 13 | min_result = result; 14 | } 15 | } 16 | } 17 | return min_result; 18 | } 19 | 20 | function p1(c, moves){ 21 | let max_result = -1; 22 | for(let i = 0; i= 0){ 24 | let result = p2(c-moves[i], moves); 25 | if(result > max_result){ 26 | max_result = result 27 | } 28 | } 29 | } 30 | return max_result; 31 | } 32 | 33 | function game(cases, moves){ 34 | for(let i = 0; i= 0: 13 | result = p1(c-m, moves) 14 | if result < min_result: 15 | min_result = result 16 | 17 | return min_result 18 | 19 | def p1(c, moves): 20 | max_result = -1 21 | for m in moves: 22 | if c-m >= 0: 23 | result = p2(c-m, moves) 24 | if result > max_result: 25 | max_result = result 26 | 27 | return max_result 28 | 29 | def game(cases, convert): 30 | for c in cases: 31 | outcome = p1(c, moves) 32 | print(convert[outcome]) 33 | 34 | game(cases, convert) -------------------------------------------------------------------------------- /5_stone_game/stone_game.rs: -------------------------------------------------------------------------------- 1 | /*Rust implementation for the Stone Game Problem*/ 2 | 3 | enum Convert {First = 1, Second = -1} 4 | 5 | fn p1(c: i32, moves: Vec) -> i32 { 6 | let mut max_result = Convert::Second as i32; 7 | let mut result: i32; 8 | for m in &moves { 9 | if c - *m >= 0 { 10 | result = p2(c-m, moves.to_vec()); 11 | if result > max_result { max_result = result; } 12 | } 13 | } 14 | max_result 15 | } 16 | 17 | fn p2(c: i32, moves: Vec) -> i32 { 18 | let mut min_result = Convert::First as i32; 19 | let mut result: i32; 20 | for m in &moves { 21 | if c - *m >= 0 { 22 | result = p1(c-m, moves.to_vec()); 23 | if result < min_result { min_result = result; } 24 | } 25 | } 26 | min_result 27 | } 28 | 29 | fn main() { 30 | let cases = [1,2,3,4,5,6,7,10].to_vec(); 31 | let moves = [2,3,5].to_vec(); 32 | let mut outcome: i32; 33 | 34 | for c in cases { 35 | outcome = p1(c, moves.to_vec()); 36 | match outcome { 37 | 1 => println!("{}:\tFirst", c), 38 | -1 => println!("{}:\tSecond", c), 39 | _ => panic!("An internal error occurred") 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /6_write_txt/data.txt: -------------------------------------------------------------------------------- 1 | A cypherpunk is any activist advocating widespread use of strong cryptography and privacy-enhancing technologies as a route to social and political change. 2 | 3 | John Gilmore said he wanted "a guarantee -- with physics and mathematics, not with laws -- that we can give ourselves real privacy of personal communications." 4 | 5 | Such guarantees require strong cryptography, so cypherpunks are fundamentally opposed to government policies attempting to control the usage or export of cryptography, which remained an issue throughout the late 1990s. The Cypherpunk Manifesto stated "Cypherpunks deplore regulations on cryptography, for encryption is fundamentally a private act." 6 | 7 | Arguably, the possibility of anonymous speech and publication is vital for an open society, an essential requirement for genuine freedom of speech — this was the position of most cypherpunks. 8 | 9 | An important set of discussions concerns the use of cryptography in the presence of oppressive authorities. As a result, Cypherpunks have discussed and improved steganographic methods that hide the use of crypto itself, or that allow interrogators to believe that they have forcibly extracted hidden information from a subject. For instance, Rubberhose was a tool that partitioned and intermixed secret data on a drive with fake secret data, each of which accessed via a different password. Interrogators, having extracted a password, are led to believe that they have indeed unlocked the desired secrets, whereas in reality the actual data is still hidden. In other words, even its presence is hidden. Likewise, cypherpunks have also discussed under what conditions encryption may be used without being noticed by network monitoring systems installed by oppressive regimes. 10 | 11 | Cypherpunks write code. We know that someone has to write software to defend privacy, and... we're going to write it. -------------------------------------------------------------------------------- /6_write_txt/readme.md: -------------------------------------------------------------------------------- 1 | # Write .txt 2 | 3 | ### The Problem 4 | 5 | Open a .txt file called data.txt and read it. Then write both the first and the last line separated by an extra blank line. 6 | 7 | The new .txt file should be named write_x.txt where **x** is the extension of the files written in that language. 8 | 9 | ### Input >> Desired Output / Test Cases 10 | 11 | Python => write_py.txt 12 | 13 | JavaScript => write_js.txt 14 | 15 | Go => write_go.txt -------------------------------------------------------------------------------- /6_write_txt/write_c.txt: -------------------------------------------------------------------------------- 1 | A cypherpunk is any activist advocating widespread use of strong cryptography and privacy-enhancing technologies as a route to social and political change. 2 | 3 | Cypherpunks write code. We know that someone has to write software to defend privacy, and... we're going to write it. 4 | -------------------------------------------------------------------------------- /6_write_txt/write_go.txt: -------------------------------------------------------------------------------- 1 | A cypherpunk is any activist advocating widespread use of strong cryptography and privacy-enhancing technologies as a route to social and political change. 2 | 3 | Cypherpunks write code. We know that someone has to write software to defend privacy, and... we're going to write it. -------------------------------------------------------------------------------- /6_write_txt/write_pl.txt: -------------------------------------------------------------------------------- 1 | A cypherpunk is any activist advocating widespread use of strong cryptography and privacy-enhancing technologies as a route to social and political change. 2 | 3 | Cypherpunks write code. We know that someone has to write software to defend privacy, and... we're going to write it. -------------------------------------------------------------------------------- /6_write_txt/write_py.txt: -------------------------------------------------------------------------------- 1 | A cypherpunk is any activist advocating widespread use of strong cryptography and privacy-enhancing technologies as a route to social and political change. 2 | 3 | Cypherpunks write code. We know that someone has to write software to defend privacy, and... we're going to write it. -------------------------------------------------------------------------------- /6_write_txt/write_txt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program written for ROSETTA_STONE 3 | * How to Execute: 4 | * You will need a compiler, best one is GCC 5 | * In your preferred CLI type gcc write_txt.c -o output 6 | * If you don't see any errors or warnings, your program compiled perfectly 7 | * Run your program by ./output 8 | * Any issues, reach me on Github @mayurdw 9 | */ 10 | 11 | // Include the required header files 12 | #include // Required for input output operations 13 | #include // Required for string comparisons 14 | 15 | //function declarations 16 | int ReadFirstLine( void ); 17 | int ReadLastLine( void ); 18 | void WriteLine( char *pszLine ); 19 | 20 | // Preprocessor defines 21 | #define BUFFER_SIZE 1024 22 | #define NEW_LINE "\n" 23 | #define DATA_FILE "data.txt" 24 | #define OUTPUT_FILE "write_c.txt" 25 | 26 | void main() 27 | { 28 | int iReturn = 0; 29 | int iReturn2 = 0; 30 | 31 | iReturn = ReadFirstLine(); 32 | if ( iReturn != 0 ) 33 | { 34 | //Reading & writing first line successful 35 | iReturn2 = ReadLastLine(); 36 | } 37 | 38 | if ( !iReturn || !iReturn2 ) 39 | { 40 | printf( "Operation failed!!\nReturn values of first line and second line are %i %i", iReturn, iReturn2 ); 41 | } 42 | 43 | return; 44 | } 45 | 46 | int ReadFirstLine() 47 | { 48 | FILE *pDataFile = NULL; 49 | char szFirstLine[ BUFFER_SIZE + 1] = { 0, }; 50 | char *pszFirstSentenceEnd = NULL; 51 | 52 | pDataFile = fopen( DATA_FILE, "r" ); 53 | 54 | if ( pDataFile == NULL ) 55 | { 56 | //error opening file, exit early 57 | return 0; 58 | } 59 | // file exists 60 | // fgets will stop reading as soon as it reaches a NEW_LINE char 61 | fgets( szFirstLine, BUFFER_SIZE, ( FILE *) pDataFile ); 62 | 63 | pszFirstSentenceEnd = strstr( szFirstLine, NEW_LINE ); 64 | 65 | if ( pszFirstSentenceEnd ) 66 | { 67 | // we have the first line now write it to the output file 68 | WriteLine( szFirstLine ); 69 | } 70 | 71 | fclose( pDataFile ); 72 | return 1; 73 | } 74 | 75 | void WriteLine( char *pszLine ) 76 | { 77 | FILE *OutputFile = NULL; 78 | 79 | if ( pszLine == NULL ) 80 | return; 81 | 82 | OutputFile = fopen( OUTPUT_FILE, "a"); 83 | 84 | if ( OutputFile == NULL ) 85 | return; 86 | 87 | fputs( pszLine, OutputFile ); 88 | fputs( NEW_LINE, OutputFile ); 89 | 90 | fclose( OutputFile ); 91 | return; 92 | } 93 | 94 | int ReadLastLine() 95 | { 96 | FILE *pDataFile = NULL; 97 | char szLastLine[BUFFER_SIZE + 1] = { 0, }; 98 | char *pszSentenceEnd = NULL; 99 | 100 | pDataFile = fopen( DATA_FILE, "r" ); 101 | 102 | if( pDataFile == NULL ) 103 | { 104 | //Error opening file 105 | return 0; 106 | } 107 | 108 | //We have to keep reading till we reach End of file where there is no NEW_LINE character 109 | do 110 | { 111 | fgets( szLastLine, BUFFER_SIZE, ( FILE * )pDataFile ); 112 | pszSentenceEnd = strstr( szLastLine, NEW_LINE ); 113 | } while( pszSentenceEnd != NULL ); 114 | 115 | // we have reached the last line, make sure we have something 116 | if ( strlen( szLastLine ) > 0 ) 117 | { 118 | WriteLine( szLastLine ); 119 | } 120 | 121 | fclose( pDataFile ); 122 | return 1; 123 | } -------------------------------------------------------------------------------- /6_write_txt/write_txt.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "strings" 7 | "os" 8 | ) 9 | 10 | func read_txt(name string) (string,string){ // 11 | b, err := ioutil.ReadFile(name) // just pass the file name 12 | if err != nil { 13 | fmt.Print(err) 14 | } 15 | str := string(b) // convert content to a 'string' 16 | chunks := strings.Split(str, "\n") 17 | 18 | first,last := chunks[0], chunks[len(chunks)-1] 19 | 20 | return first,last 21 | } 22 | 23 | func write_txt(first string, second string) bool{ 24 | file, err := os.Create("write_go.txt") 25 | if err != nil { 26 | fmt.Print(err) 27 | } 28 | defer file.Close() 29 | 30 | fmt.Fprintln(file, first) 31 | fmt.Fprintf(file, "\n") 32 | fmt.Fprintf(file, second) 33 | 34 | return true 35 | } 36 | 37 | func main() { 38 | first,second := read_txt("data.txt") 39 | write_txt(first,second) 40 | fmt.Println("Success.") 41 | } 42 | -------------------------------------------------------------------------------- /6_write_txt/write_txt.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /6_write_txt/write_txt.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | open IN, "<", "data.txt" || die "Couldn't open data.txt for reading\n"; 7 | 8 | my $is_first = 1; 9 | my ($first, $last); 10 | while() { 11 | if ($is_first) { 12 | $first = $_; 13 | $is_first = 0; 14 | } 15 | $last = $_; 16 | } 17 | close IN; 18 | 19 | open OUT, ">", "write_pl.txt" || die "Couldn't open write_pl.txt for writing.\n"; 20 | 21 | print OUT "$first\n$last"; 22 | 23 | close OUT; 24 | -------------------------------------------------------------------------------- /6_write_txt/write_txt.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | # Python implementation of the write_txt code for Rosetta Stone 3 | # Author: Eric Alcaide 4 | 5 | def read_txt(name="data.txt"): 6 | # Automatically closes the file once outside of it 7 | with open(name, 'r') as f: 8 | r = f.readlines() 9 | first,last = r[0], r[-1] 10 | 11 | return first,last 12 | 13 | def write_txt(first, second="Bye bye .txt file"): 14 | with open('write_py.txt', 'w') as f: 15 | f.write(first) 16 | f.write('\n'+second) 17 | 18 | def main(): 19 | first, second = read_txt() 20 | write_txt(first,second) 21 | 22 | if __name__ == "__main__": 23 | main() -------------------------------------------------------------------------------- /6_write_txt/write_txt.rs: -------------------------------------------------------------------------------- 1 | // Rust implementation of the write_txt code for Rosetta Stone 2 | use std::fs::File; 3 | use std::fs::OpenOptions; 4 | use std::io::Read; 5 | use std::io::prelude::*; 6 | 7 | fn main() { 8 | let mut file_in = File::open("data.txt").expect("opening data file"); 9 | File::create("write_rs.txt").expect("Creating output file"); 10 | let mut file_out = OpenOptions::new().write(true).append(true).open("write_rs.txt").expect("Opening output file"); 11 | let mut text = String::new(); 12 | file_in.read_to_string(&mut text).expect("reading file"); 13 | for line in text.lines() { 14 | writeln!(file_out, "{}", line).expect("writing first line to output file"); 15 | break; 16 | } 17 | writeln!(file_out); // writing blank line! 18 | for line in text.lines().rev() { 19 | write!(file_out, "{}", line).expect("writing last line to output file"); 20 | break; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /7_first_hashes/first_hashes.go: -------------------------------------------------------------------------------- 1 | // Go code for a hashing practice (Rosetta Stone) 2 | // Author: Eric Alcaide 3 | package main 4 | 5 | import ( 6 | "fmt" 7 | "crypto/sha512" 8 | "bufio" 9 | "os" 10 | ) 11 | 12 | func req_string() string{ 13 | reader := bufio.NewReader(os.Stdin) 14 | word, _ := reader.ReadString('\n') 15 | 16 | sha_512 := sha512.New() 17 | sha_512.Write([]byte(word)) 18 | sha_512_out := fmt.Sprintf("%x", sha_512.Sum(nil)) 19 | 20 | return sha_512_out 21 | } 22 | 23 | func main() { 24 | fmt.Print("Introduce password: ") 25 | word := req_string() 26 | 27 | for { 28 | fmt.Print("Repeat password: ") 29 | new_w := req_string() 30 | 31 | if new_w == word{ 32 | fmt.Println("Good job!") 33 | break 34 | }else{ 35 | fmt.Println("Passwords don't match. Try again.") 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /7_first_hashes/first_hashes.java: -------------------------------------------------------------------------------- 1 | import java.security.MessageDigest; 2 | import java.security.NoSuchAlgorithmException; 3 | import java.util.Scanner; 4 | 5 | public class first_hashes { 6 | public static void main(final String args[]) throws NoSuchAlgorithmException { 7 | System.out.println("Enter password"); 8 | final Scanner scan = new Scanner(System.in); 9 | String user_input = scan.nextLine(); 10 | final String hashed_password = hash_it(user_input); 11 | 12 | for (int i = 0; i < 3; i++) { 13 | System.out.println("Retype password. You've " + (3 - i) + " attempts remaining."); 14 | user_input = scan.nextLine(); 15 | final String hashed_retyped_password = hash_it(user_input); 16 | 17 | if (hashed_retyped_password.equals(hashed_password)) { 18 | System.out.println("Good job!"); 19 | scan.close(); 20 | return; 21 | } 22 | } 23 | System.out.println("Passwords didn't match."); 24 | scan.close(); 25 | } 26 | 27 | public static String hash_it(final String input) throws NoSuchAlgorithmException { 28 | final MessageDigest md = MessageDigest.getInstance("SHA-512"); 29 | md.update(input.getBytes()); 30 | 31 | final byte byteData[] = md.digest(); 32 | final StringBuffer sb = new StringBuffer(); 33 | for (final byte element : byteData) { 34 | sb.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1)); 35 | } 36 | return sb.toString(); 37 | } 38 | } -------------------------------------------------------------------------------- /7_first_hashes/first_hashes.js: -------------------------------------------------------------------------------- 1 | // JavaScript code for a hashing practice (Rosetta Stone) 2 | // Author: Eric Alcaide 3 | 4 | // This is a code skeleton. NOT WORKING! 5 | 6 | function req_string(){ 7 | let word = ; 8 | return word 9 | } 10 | 11 | function main(){ 12 | console.log("Introduce password: ") 13 | word = req_string() 14 | 15 | while true{ 16 | console.log("Repeat password: ") 17 | new_w = req_string() 18 | 19 | if new_w == word{ 20 | console.log("Good job!") 21 | break; 22 | }else{ 23 | console.log("Passwords don't match. Try again.") 24 | } 25 | } 26 | } 27 | 28 | main(); -------------------------------------------------------------------------------- /7_first_hashes/first_hashes.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | # Python code for a hashing practice (Rosetta Stone) 3 | # Author: Eric Alcaide 4 | 5 | import hashlib 6 | 7 | def req_string(): 8 | word = input().encode('utf-8') 9 | return hashlib.sha512(word).hexdigest() 10 | 11 | def main(): 12 | print("Introduce password:") 13 | word = req_string() 14 | 15 | while True: 16 | print("Repeat password:") 17 | new = req_string() 18 | 19 | if new == word: 20 | print("Good job!") 21 | break 22 | else: 23 | print("Passwords don't match. Try again.") 24 | 25 | if __name__ == "__main__": 26 | main() -------------------------------------------------------------------------------- /7_first_hashes/readme.md: -------------------------------------------------------------------------------- 1 | # First Hashing Experiment 2 | 3 | ### The Problem 4 | 5 | Ask the user for an input and apply the sha-2 (sha-512) hash on it. Store it in a variable. 6 | 7 | Ask the user for the same input. Hash it the same way and compare it to the previous variable. If the hash matches, return a "Good job!". If it does not, return a "Try again." and allow a retry. 8 | 9 | ### Input >> Desired Output / Test Cases 10 | 11 | Hello -> 3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315 + Hello -> 3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315 = Good job! 12 | 13 | Hashing Practice -> a40fac78c9002b340f42026375f3726472208e2e1bfbde4ecb38ba2daa8c6caa3c52cad985d3230b1d784c2a26508ebf0f6b80400334febca56bd5ff8b1dce86 + Hashing Practice -> a40fac78c9002b340f42026375f3726472208e2e1bfbde4ecb38ba2daa8c6caa3c52cad985d3230b1d784c2a26508ebf0f6b80400334febca56bd5ff8b1dce86 = Good job! 14 | 15 | Hello Privacy -> 1cc8ced45c45d97604d1c43587bdb2627038074ac90ce809a0e85da6d63bedc5a3be8c81188cc5f6262c15bf0100b866b984c67f113a7c5203db0cffea19c57c + Hello -> 3615f80c9d293ed7402687f94b22d58e529b8cc7916f8fac7fddf7fbd5af4cf777d3d795a7a00a16bf7e7f3fb9561ee9baae480da9fe7a18769e71886b03f315 = Try again. 16 | -------------------------------------------------------------------------------- /8_caesar_cipher/caesar_cipher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, string argv[]) { 8 | //vars go here 9 | string plaintext; 10 | int length, k; 11 | 12 | //make sure that main has 2 arguments 13 | while (argc != 2) { 14 | printf("Does not compute. Enter a key to encypher."); 15 | return 1; 16 | } 17 | 18 | //if it does have 2 args 19 | if (argc == 2) { 20 | printf("plaintext: "); //prompt for message to encrypt 21 | plaintext = get_string(); 22 | length = strlen(plaintext); //get the length to iterate through later 23 | k = atoi(argv[1]); //turn string into a number so I can work with it 24 | printf("ciphertext: "); //wont pass check50 without this tidbit 25 | 26 | for (int i = 0; i < length; i++) { //iterate through string 27 | if isupper(plaintext[i]) { //if it's capital, keep it capital 28 | char C = ((((plaintext[i] + k) - 65) % 26) + 65); 29 | printf("%c", C); 30 | } 31 | else if islower(plaintext[i]) { //if it's lowercase, keep it that way 32 | char c = ((((plaintext[i] + k) - 97) % 26) + 97); 33 | printf("%c", c); 34 | } 35 | else { 36 | printf("%c", plaintext[i]); //print any other text 37 | } 38 | } 39 | } 40 | printf("\n"); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /8_caesar_cipher/caesar_cipher.py: -------------------------------------------------------------------------------- 1 | import string 2 | from functools import reduce 3 | 4 | 5 | def caesar(plain, shift): 6 | def substitute(acc, letter): 7 | if not letter.isalpha(): 8 | return acc + letter 9 | 10 | idx = alphabet.find(letter) 11 | letters = alphabet[:26] if idx < 26 else alphabet[26:] 12 | return acc + letters[(idx + shift) % 26] 13 | 14 | alphabet = string.ascii_letters 15 | 16 | result = reduce( 17 | substitute, 18 | plain, 19 | '' 20 | ) 21 | 22 | return result 23 | 24 | 25 | def main(): 26 | assert caesar('This is a caesar cipher example', shift=0) == 'This is a caesar cipher example' 27 | assert caesar('A not so long string', shift=2) == 'C pqv uq nqpi uvtkpi' 28 | assert caesar('Negative shift', shift=-1) == 'Mdfzshud rghes' 29 | 30 | 31 | if __name__ == '__main__': 32 | main() 33 | -------------------------------------------------------------------------------- /8_caesar_cipher/caesar_cipher.rb: -------------------------------------------------------------------------------- 1 | class AssertionError < RuntimeError; end 2 | def assert █ raise AssertionError unless yield; end 3 | 4 | def caesar(plain, shift:) 5 | alphabets = ['A'..'Z', 'a'..'z'].map do |alphabet| 6 | Hash[alphabet.map.with_index.to_a] 7 | end 8 | 9 | plain.chars.map do |char| 10 | alphabet = alphabets.detect { |alphabet| alphabet.include? char } 11 | next char unless alphabet 12 | char_index = alphabet[char] 13 | new_char_index = (char_index + shift) % alphabet.size 14 | alphabet.keys[new_char_index] 15 | end.join 16 | end 17 | 18 | assert { caesar('This is a caesar cipher example', shift: 0) == 'This is a caesar cipher example' } 19 | assert { caesar('A not so long string', shift: 2) == 'C pqv uq nqpi uvtkpi' } 20 | assert { caesar('Negative shift', shift: -1) == 'Mdfzshud rghes' } 21 | -------------------------------------------------------------------------------- /9_quick_sort/quick_sort.php: -------------------------------------------------------------------------------- 1 | $pivot) 17 | { 18 | $gt[] = $val; 19 | } 20 | } 21 | return array_merge(quick_sort($loe),array($pivot_key=>$pivot),quick_sort($gt)); 22 | } 23 | 24 | $my_array = array(3, 0, 2, 5, -1, 4, 1); 25 | echo 'Original Array : '.implode(',',$my_array).'\n'; 26 | $my_array = quick_sort($my_array); 27 | echo 'Sorted Array : '.implode(',',$my_array); 28 | ?> -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rosetta Project 2 | 3 | This project is intended to be a programming **Rosetta Stone**. Here you'll find algorithms in various coding languages. 4 | 5 | This project is born with the idea that the best way to learn how to code is coding itself. Yes, **coding is a learning by doing task**. 6 | 7 | ## Organization 8 | 9 | Algorithms will be organized in folders. Each folder will contain a **readme.md** explaining the problem and the algorithm implementations in various languages. 10 | 11 | ## Languages 12 | 13 | The first version will include the following languages: 14 | * Python 15 | * JavaScript 16 | * Go 17 | 18 | And all others added by contributors. More languages will be continously added with Pull Requests. Feel free to fork and add another language! 19 | 20 | ## Contribute 21 | 22 | You're strongly encouraged to contribute and add your own language! Open/close issues, fork the repo and make a Pull request! All ideas are welcome. 23 | 24 | By participating in this project, you agree to abide by the thoughtbot [code of conduct](https://thoughtbot.com/open-source-code-of-conduct) 25 | 26 | ## Meta 27 | 28 | * **Author's Profile**: [Eric Alcaide](https://github.com/hypnopump/) 29 | * **Twitter**: [@eric_alcaide](https://twitter.com/eric_alcaide) 30 | * **Email**: ericalcaide1@gmail.com 31 | --------------------------------------------------------------------------------