├── .gitignore ├── CONTRIBUTING.md ├── CONTRIBUTOR ├── LICENSE ├── Makefile ├── README.md ├── c └── sort │ ├── main.c │ └── sort-in-one-loop.c ├── go ├── manipulation │ ├── main.go │ ├── pig_latin.go │ ├── remove_duplicate_from_sorted_array.go │ ├── remove_duplicate_from_unsorted_array.go │ └── reverse_random_array.go ├── recursion │ ├── block_and_iron.go │ └── main.go ├── sort │ ├── bubble-sort.go │ ├── insertion-sort.go │ ├── main.go │ ├── selection-sort.go │ └── sort-in-one-loop.go └── string │ ├── common_subsequence.go │ ├── longest_common_subsequence.go │ ├── main.go │ ├── reverse_string.go │ └── subsequence.go ├── java ├── recursion │ └── TriangleRecursion.java ├── searching │ ├── BinarySearching.java │ └── SequentialSearching.java └── sort │ ├── BubbleSort.java │ ├── InsertionSort.java │ └── SelectionSort.java └── python └── dictionary ├── Dynamically_Importing_To_Dictionary.py ├── Searching_Dictionary_Inside_List.py └── sample ├── __init__.py ├── movies.py └── musics.py /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | main 3 | *.class 4 | .idea/ 5 | *.pyc -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | When contributing to this repository, please first discuss the change you wish to make via issue, 4 | email, or any other method with the owners of this repository before making a change. 5 | 6 | Please note we have a code of conduct, please follow it in all your interactions with the project. 7 | 8 | ## Pull Request Process 9 | 10 | 1. Ensure any install or build dependencies are removed before the end of the layer when contributing. 11 | 2. Update the README.md with details of changes, addition, improvement of a code. 12 | 3. Please follow the [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/) guide. 13 | 4. You may merge the Pull Request in once you have the sign-off of one other developers, or if you 14 | do not have permission to do that, you may request the reviewer or maintainer to merge it for you. 15 | 16 | ## Code of Conduct 17 | 18 | ### Our Pledge 19 | 20 | In the interest of fostering an open and welcoming environment, we as 21 | contributors and maintainers pledge to making participation in our project and 22 | our community a harassment-free experience for everyone, regardless of age, body 23 | size, disability, ethnicity, gender identity and expression, level of experience, 24 | nationality, personal appearance, race, religion, or sexual identity and 25 | orientation. 26 | 27 | ### Our Standards 28 | 29 | Examples of behavior that contributes to creating a positive environment 30 | include: 31 | 32 | * Using welcoming and inclusive language 33 | * Being respectful of differing viewpoints and experiences 34 | * Gracefully accepting constructive criticism 35 | * Focusing on what is best for the community 36 | * Showing empathy towards other community members 37 | 38 | Examples of unacceptable behavior by participants include: 39 | 40 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 41 | * Trolling, insulting/derogatory comments, and personal or political attacks 42 | * Public or private harassment 43 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 44 | * Other conduct which could reasonably be considered inappropriate in a 45 | professional setting 46 | 47 | ### Our Responsibilities 48 | 49 | Project maintainers are responsible for clarifying the standards of acceptable 50 | behavior and are expected to take appropriate and fair corrective action in 51 | response to any instances of unacceptable behavior. 52 | 53 | Project maintainers have the right and responsibility to remove, edit, or 54 | reject comments, commits, code, wiki edits, issues, and other contributions 55 | that are not aligned to this Code of Conduct, or to ban temporarily or 56 | permanently any contributor for other behaviors that they deem inappropriate, 57 | threatening, offensive, or harmful. 58 | 59 | ### Scope 60 | 61 | This Code of Conduct applies both within project spaces and in public spaces 62 | when an individual is representing the project or its community. Examples of 63 | representing a project or community include using an official project e-mail 64 | address, posting via an official social media account, or acting as an appointed 65 | representative at an online or offline event. Representation of a project may be 66 | further defined and clarified by project maintainers. 67 | 68 | ### Enforcement 69 | 70 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 71 | reported by contacting the project team via telegram @insomnius. All 72 | complaints will be reviewed and investigated and will result in a response that 73 | is deemed necessary and appropriate to the circumstances. The project team is 74 | obligated to maintain confidentiality with regard to the reporter of an incident. 75 | Further details of specific enforcement policies may be posted separately. 76 | 77 | Project maintainers who do not follow or enforce the Code of Conduct in good 78 | faith may face temporary or permanent repercussions as determined by other 79 | members of the project's leadership. 80 | 81 | ### Attribution 82 | 83 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 84 | available at [http://contributor-covenant.org/version/1/4][version] 85 | 86 | [homepage]: http://contributor-covenant.org 87 | [version]: http://contributor-covenant.org/version/1/4/ -------------------------------------------------------------------------------- /CONTRIBUTOR: -------------------------------------------------------------------------------- 1 | Name - Github Username - Telegram Username 2 | M Arief Rahman - @insomnius - @insomnius -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Muhammad Arief Rahman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | c_sort: 2 | @gcc -o c/sort/main c/sort/main.c 3 | @./c/sort/main 4 | 5 | go_sort: 6 | @go run go/sort/* 7 | 8 | go_recursion: 9 | @go run go/recursion/* 10 | 11 | go_manipulation: 12 | @go run go/manipulation/* 13 | 14 | go_string: 15 | @go run go/string/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code Geek 2 | 3 | This repo is dedicated to store all of programing interview test from a very simple one to a very hardest one, algorithm implementation, mini console game like tetris or pacman maybe and all of geeky code. The aim of this repo is to help people to learn algorithm better and find solution that maybe useful in the future. 4 | 5 | ## Prerequisites 6 | 7 | In order to run all example in this repository you need to have 8 | 9 | - Linux 10 | - General 11 | - `make` command - GNU make utility to maintain groups of programs 12 | - C 13 | - C programmming language 14 | - `gcc` - GNU project C and C++ compiler 15 | - Go 16 | - [Go programming language](https://golang.org/doc/install) 17 | - `go` command 18 | - Java 19 | - [Java programming language](https://www.java.com/en/download/help/download_options.xml) 20 | - `javac` and `java` - Java project for compile and running 21 | - Python or Python3 22 | - Python Programming language 23 | - `python` or `python3` - Python command for executed python file. 24 | 25 | - macOS 26 | - General 27 | - `brew` command - Brew or Homebrew calls itself The missing package manager for macOS and is an essential tool for any developer. 28 | - C Family Languages 29 | - `clang` - Xcode shipped with clang compiler for C, C++, Objective-C, and Swift for free. 30 | - Xcode required for most macOS development tools to run. 31 | - Go 32 | - `go` command 33 | - Java 34 | - JDK required for compile and running Java project 35 | 36 | macOS setup tutorial for development in various tools can be found in [macOS Setup - Sourabh Bajaj](http://sourabhbajaj.com/mac-setup/). 37 | 38 | ## Table of Contents 39 | 40 | - C 41 | - Sort 42 | - [Sort in one loop](https://github.com/insomnius/programming-test-interview/blob/master/c/sort/sort-in-one-loop.c) 43 | 44 | - Go 45 | - Sort 46 | - [Sort in one loop](https://github.com/insomnius/programming-test-interview/blob/master/go/sort/sort-in-one-loop.go) 47 | - [Bubble sort](https://github.com/insomnius/programming-test-interview/blob/master/go/sort/bubble-sort.go) 48 | - [Selection sort](https://github.com/insomnius/programming-test-interview/blob/master/go/sort/selection-sort.go) 49 | - [Insertion sort](https://github.com/insomnius/programming-test-interview/blob/master/go/sort/insertion-sort.go) 50 | - Manipulation 51 | - [Remove duplicate from sorted array](https://github.com/insomnius/programming-test-interview/blob/master/go/manipulation/remove_duplicate_from_sorted_array.go) 52 | - [Remove duplicate from unsorted array](https://github.com/insomnius/programming-test-interview/blob/master/go/manipulation/remove_duplicate_from_unsorted_array.go) 53 | - [Reverse random array](https://github.com/insomnius/programming-test-interview/blob/master/go/manipulation/reverse_random_array.go) 54 | - [Pig Latin](https://github.com/insomnius/programming-test-interview/blob/master/go/manipulation/pig_latin.go.go) 55 | - Recursion 56 | - [Block and Iron](https://github.com/insomnius/programming-test-interview/blob/master/go/recursion/block_and_iron.go) 57 | - String 58 | - [Reverse String](https://github.com/insomnius/programming-test-interview/blob/master/go/string/reverse_string.go) 59 | - [Subsequence](https://github.com/insomnius/programming-test-interview/blob/master/go/string/subsequence.go) 60 | - [Longest Common Subsequence](https://github.com/insomnius/programming-test-interview/blob/master/go/string/longest_common_subsequence.go) 61 | - [Common Subsequence](https://github.com/insomnius/programming-test-interview/blob/master/go/string/common_subsequence.go) 62 | 63 | - Java 64 | - Sort 65 | - [Bubble sort](https://github.com/insomnius/programming-test-interview/blob/master/java/sort/BubbleSort.java) 66 | - [Selection sort](https://github.com/insomnius/programming-test-interview/blob/master/java/sort/SelectionSort.java) 67 | - [Insertion sort](https://github.com/insomnius/programming-test-interview/blob/master/java/sort/InsertionSort.java) 68 | - Searching 69 | - [Sequential searching](https://github.com/insomnius/programming-test-interview/blob/master/java/searching/SequentialSearching.java) 70 | - [Binary searching](https://github.com/insomnius/programming-test-interview/blob/master/java/searching/BinarySearching.java) 71 | - Recursion 72 | - [Triangle recursion](https://github.com/insomnius/programming-test-interview/blob/master/java/recursion/TriangleRecursion.java) 73 | 74 | - Python 75 | - dictionary 76 | - [Dynamically Importing And Convert into Dictionary]() 77 | - [Searching Dictionary Inside the List based on Keyword Dictionary]() 78 | ## How to run 79 | 80 | ### Go 81 | 82 | - Sort 83 | - To run all of go sort example please run `go run go/sort/*` and you will get all of the output sample or just simply use `make go_sort`. 84 | - Manipulation 85 | - To run all of go sort example please run `go run go/manipulation/*` and you will get all of the output sample or just simply use `make go_manipulation`. 86 | - Recursion 87 | - To run all of go sort example please run `go run go/recursion/*` and you will get all of the output sample or just simply use `make go_recursion`. 88 | - String 89 | - To run all of go sort example please run `go run go/string/*` and you will get all of the output sample or just simply use `make go_string`. 90 | 91 | ### C 92 | 93 | - Sort 94 | - use command `gcc -o c/sort/main c/sort/main.c` to generate compiled file and then run it with `./c/sort/main` or just simply use `make c_sort` 95 | 96 | ### Java 97 | - use command `javac filename.java` to generate compiled file and then run it with `java filename.class`. 98 | 99 | ### Python 100 | - use command `python filename.py arg arg ...` to execute the python file and run it with terminal or command prompt 101 | 102 | ## Contributing 103 | 104 | Please read [CONTRIBUTING.md](https://github.com/insomnius/code-geek/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. Check all of people that already contribute in this project in [here](https://github.com/insomnius/code-geek/blob/master/CONTRIBUTOR). 105 | 106 | ## Authors 107 | 108 | - **Insomnius** - Initial work, main GO contributor. 109 | - **Kzulfazriawan** - python contributor. 110 | 111 | ## License 112 | 113 | This project is licensed under the MIT License - see the [LICENSE](https://github.com/insomnius/code-geek/blob/master/LICENSE) file for details. 114 | -------------------------------------------------------------------------------- /c/sort/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sort-in-one-loop.c" 3 | 4 | void hr(); 5 | void measure(); 6 | void sort_in_one_loop(); 7 | 8 | int main() 9 | { 10 | printf("\t\tCode Geek\n\n"); 11 | printf("\t\tLanguage: C\n"); 12 | printf("\t\tCategory: Sorting\n"); 13 | hr(); 14 | 15 | hr(); 16 | measure(sort_in_one_loop); 17 | hr(); 18 | return 0; 19 | } 20 | 21 | void hr() 22 | { 23 | printf("============================================================\n"); 24 | } 25 | 26 | void measure(void *f()) 27 | { 28 | (*f)(); 29 | } 30 | -------------------------------------------------------------------------------- /c/sort/sort-in-one-loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Here is a simple trick to do an array sort in just one loop. 5 | // Level: Easy 6 | void sort_in_one_loop() 7 | { 8 | printf("sort in one loop\n"); 9 | 10 | int array_length = 50; 11 | int array_number[array_length]; 12 | int i; 13 | int x; 14 | 15 | printf("random number:"); 16 | for (i = 0; i < array_length; i++) 17 | { 18 | array_number[i] = rand() % 10; 19 | printf(" %d", array_number[i]); 20 | } 21 | printf("\n"); 22 | 23 | i = 0; 24 | 25 | while (i < array_length) 26 | { 27 | if (i != (array_length - 1) && i >= 0 && array_number[i] > array_number[i + 1]) 28 | { 29 | x = array_number[i]; 30 | array_number[i] = array_number[i + 1]; 31 | array_number[i + 1] = x; 32 | i -= 2; 33 | } 34 | i++; 35 | } 36 | 37 | printf("sorted number: "); 38 | for (i = 0; i < array_length; i++) 39 | { 40 | printf(" %d", array_number[i]); 41 | } 42 | printf("\n"); 43 | } 44 | -------------------------------------------------------------------------------- /go/manipulation/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | fmt.Printf("\t\tCode Geek\n\n") 10 | fmt.Println("Language: Go") 11 | fmt.Println("Category: Manipulation") 12 | hr() 13 | 14 | measure(remove_duplicate_from_sorted_array) 15 | hr() 16 | measure(remove_duplicate_from_unsorted_array) 17 | hr() 18 | measure(reverse_random_array) 19 | hr() 20 | measure(pig_latin) 21 | hr() 22 | } 23 | 24 | // hr function use to print dividing line 25 | func hr() { 26 | fmt.Printf("============================================================\n") 27 | } 28 | 29 | // measure function use to measure elapsed time to run a function 30 | func measure(f func()) { 31 | start_time := time.Now() 32 | f() 33 | elapsed := float64(time.Since(start_time).Nanoseconds()) / 1000000 34 | fmt.Println("elapsed time: ", elapsed, "ms") 35 | } 36 | -------------------------------------------------------------------------------- /go/manipulation/pig_latin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | /* 6 | Pig Latin is an argot ("secret" language) in which word in english are altered to 7 | conceal the words from others not familiar with the rules. 8 | 9 | You convert a word Pig Latin by transfering the initial consonant or consonant cluster 10 | of the word to the end of the word with -ay appended to the end. if the word start with 11 | vowel (including y), append -yay to the end. For example, "pig" would become igpay 12 | (taking the 'p' and 'ay' to create a suffix) 13 | 14 | Examples: 15 | Input: say 16 | Output: aysay 17 | 18 | Input: english 19 | Output: englishyay 20 | 21 | Input: smile 22 | Output: ilesmay 23 | 24 | Level: Easy 25 | */ 26 | func pig_latin() { 27 | fmt.Println("pig latin") 28 | 29 | var bag_of_words = []string{ 30 | "when", "i", "was", "a", "young", "boy", "my", "father", "took", "me", "into", "city", "to", "see", 31 | "a", "marching", "band", 32 | } 33 | bag_of_words_length := len(bag_of_words) 34 | fmt.Println("bag of words:", bag_of_words) 35 | 36 | for i := 0; i < bag_of_words_length; i++ { 37 | fmt.Printf(`english word: "%s", `, bag_of_words[i]) 38 | bag_of_words[i] = pig_latin_converter(bag_of_words[i]) 39 | fmt.Printf(`pig latin word: "%s"%s`, bag_of_words[i], "\n") 40 | } 41 | 42 | fmt.Println("converted words:", bag_of_words) 43 | } 44 | 45 | func pig_latin_converter(word string) (pig_latin_word string) { 46 | vowel_plus_y := "aioeoy" 47 | 48 | for _, vowel := range vowel_plus_y { 49 | if string(word[0]) == string(vowel) { 50 | return word + "ay" 51 | } 52 | } 53 | 54 | first_char := string(word[0]) 55 | new_word := word[1:] + first_char 56 | return pig_latin_converter(new_word) 57 | } 58 | -------------------------------------------------------------------------------- /go/manipulation/remove_duplicate_from_sorted_array.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | // Scenario: You are given sorted array and you have to remove all of duplicate values. 8 | // Level: Easy 9 | func remove_duplicate_from_sorted_array() { 10 | fmt.Println("remove duplicate from sorted array") 11 | 12 | var array_number = [20]int{1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9} 13 | array_length := len(array_number) 14 | var temp [20]int 15 | 16 | fmt.Println("sorted random number: ", array_number) 17 | 18 | j := 0 19 | 20 | for i := 0; i < array_length-1; i++ { 21 | if array_number[i] != array_number[i+1] { 22 | temp[j] = array_number[i] 23 | j++ 24 | } 25 | } 26 | 27 | temp[j] = array_number[array_length-1] 28 | j++ 29 | result_array := make([]int, j) 30 | 31 | for i := 0; i < j; i++ { 32 | result_array[i] = temp[i] 33 | } 34 | 35 | fmt.Println("result array: ", result_array) 36 | } 37 | -------------------------------------------------------------------------------- /go/manipulation/remove_duplicate_from_unsorted_array.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | ) 7 | 8 | // Scenario: You are given unsorted array and you have to remove the duplicate values. 9 | // Level: Easy 10 | func remove_duplicate_from_unsorted_array() { 11 | fmt.Println("remove duplicate from unsorted array") 12 | 13 | var unsorted_array [30]int 14 | array_length := len(unsorted_array) 15 | 16 | for i := 0; i < array_length; i++ { 17 | unsorted_array[i] = rand.Intn(10) 18 | } 19 | 20 | fmt.Println("unsorted array: ", unsorted_array) 21 | 22 | temp := make([]int, array_length) 23 | j := 0 24 | 25 | for i := 0; i < array_length; i++ { 26 | if i == 0 { 27 | temp[j] = unsorted_array[i] 28 | j++ 29 | continue 30 | } 31 | 32 | unique := true 33 | 34 | for k := 0; k < i; k++ { 35 | if unsorted_array[i] == temp[k] { 36 | unique = false 37 | break 38 | } 39 | } 40 | 41 | if unique { 42 | temp[j] = unsorted_array[i] 43 | j++ 44 | } 45 | } 46 | 47 | result_array := make([]int, j) 48 | for i := 0; i < j; i++ { 49 | result_array[i] = temp[i] 50 | } 51 | 52 | fmt.Println("result array: ", result_array) 53 | } 54 | -------------------------------------------------------------------------------- /go/manipulation/reverse_random_array.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | ) 7 | 8 | // Scenario, you are given a random array and you have to reverse its order. 9 | // Level: Easy 10 | func reverse_random_array() { 11 | fmt.Println("reverse random array") 12 | 13 | var random_array [15]int 14 | array_length := len(random_array) 15 | 16 | for i := 0; i < array_length; i++ { 17 | random_array[i] = rand.Intn(100) 18 | } 19 | 20 | fmt.Println("random array: ", random_array) 21 | 22 | for i := 0; i < array_length; i++ { 23 | temp := random_array[i] 24 | random_array[i] = random_array[array_length-1] 25 | random_array[array_length-1] = temp 26 | array_length-- 27 | } 28 | 29 | fmt.Println("reversed_array: ", random_array) 30 | } 31 | -------------------------------------------------------------------------------- /go/recursion/block_and_iron.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | /* 8 | You are a blacksmith and you sell katana complete with its scabbard. 9 | In order to make it you must have one block of wood to make scabbard and one iron in order 10 | to create the complete katana. You must have block of wood before you own iron. 11 | Let's say that block of wood is B and the iron is I, to create one katana you must have: 12 | BI not IB. 13 | 14 | To make two katanas, you must have: 15 | BBII or BIBI not BIIB or BIBB 16 | 17 | and three katana will need: 18 | BBBIII or 19 | BBIIBI or 20 | BIBBII or 21 | BIBIBI or 22 | 23 | Your task is to collect possible values of creating katana. 24 | 25 | Level: Hard 26 | */ 27 | func block_and_iron() { 28 | fmt.Println("block and iron") 29 | 30 | var katana []string 31 | fmt.Println("2 Katana needs:", block_and_iron_converter("B", "I", "B", "", 2, katana)) 32 | fmt.Println("3 Katana needs:", block_and_iron_converter("B", "I", "B", "", 3, katana[:0])) 33 | fmt.Println("4 Katana needs:", block_and_iron_converter("B", "I", "B", "", 4, katana[:0])) 34 | fmt.Println("5 Katana needs:", block_and_iron_converter("B", "I", "B", "", 5, katana[:0])) 35 | } 36 | 37 | func block_and_iron_converter(first_word_to_combine, second_word_to_combine, first_word, combined_word string, n int, katana []string) []string { 38 | for i := 0; i < n; i++ { 39 | combined_word = combined_word + first_word_to_combine 40 | word_length := len(combined_word) 41 | 42 | life := 0 43 | for _, char := range combined_word { 44 | if string(char) == first_word { 45 | life++ 46 | continue 47 | } 48 | life-- 49 | } 50 | 51 | if life < 0 { 52 | continue 53 | } 54 | 55 | if word_length < n*2 { 56 | if first_word_to_combine == first_word && (life > 1 || life < n) && life < word_length { 57 | j := word_length - 1 58 | for i := j; i > 0; i-- { 59 | if string(combined_word[i]) != first_word { 60 | break 61 | } 62 | j-- 63 | } 64 | 65 | if j != word_length { 66 | life_check := 0 67 | for j >= 0 { 68 | if string(combined_word[j]) == first_word { 69 | life_check++ 70 | j-- 71 | continue 72 | } 73 | life_check-- 74 | j-- 75 | } 76 | 77 | if life_check != 0 { 78 | continue 79 | } 80 | } 81 | } 82 | 83 | katana = block_and_iron_converter(second_word_to_combine, first_word_to_combine, first_word, combined_word, n, katana) 84 | } else { 85 | if word_length > n*2 || life > 0 { 86 | continue 87 | } 88 | katana = append(katana, combined_word) 89 | } 90 | } 91 | 92 | return katana 93 | } 94 | -------------------------------------------------------------------------------- /go/recursion/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | fmt.Printf("\t\tCode Geek\n\n") 10 | fmt.Println("Language: Go") 11 | fmt.Println("Category: Recursive") 12 | hr() 13 | 14 | hr() 15 | measure(block_and_iron) 16 | hr() 17 | } 18 | 19 | // hr function use to print dividing line 20 | func hr() { 21 | fmt.Printf("============================================================\n") 22 | } 23 | 24 | // measure function use to measure elapsed time to run a function 25 | func measure(f func()) { 26 | start_time := time.Now() 27 | f() 28 | elapsed := float64(time.Since(start_time).Nanoseconds()) / 1000000 29 | fmt.Println("elapsed time: ", elapsed, "ms") 30 | } 31 | -------------------------------------------------------------------------------- /go/sort/bubble-sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | ) 7 | 8 | // Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, 9 | // compares adjacent pairs and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. 10 | // The algorithm, which is a comparison sort, is named for the way smaller or larger elements "bubble" to the top of the list. 11 | // Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. 12 | // Bubble sort can be practical if the input is in mostly sorted order with some out-of-order elements nearly in position. 13 | // https://en.wikipedia.org/wiki/Bubble_sort 14 | // Level: Easy 15 | func bubble_sort() { 16 | fmt.Println("bubble sort") 17 | 18 | var array_number [50]int 19 | array_length := len(array_number) 20 | 21 | for i := 0; i < array_length; i++ { 22 | array_number[i] = rand.Intn(100) 23 | } 24 | 25 | fmt.Println("random number: ", array_number) 26 | 27 | for array_length > 0 { 28 | new_length := 0 29 | 30 | for i := 1; i < array_length; i++ { 31 | x := array_number[i-1] 32 | y := array_number[i] 33 | 34 | if x > y { 35 | array_number[i-1] = y 36 | array_number[i] = x 37 | new_length = i 38 | } 39 | } 40 | 41 | array_length = new_length 42 | } 43 | 44 | fmt.Println("sorted number: ", array_number) 45 | } 46 | -------------------------------------------------------------------------------- /go/sort/insertion-sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | ) 7 | 8 | // Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. 9 | // It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. 10 | // https://en.wikipedia.org/wiki/Insertion_sort 11 | // Level: Easy 12 | func insertion_sort() { 13 | fmt.Println("insertion sort") 14 | 15 | var array_number [50]int 16 | array_length := len(array_number) 17 | 18 | for i := 0; i < array_length; i++ { 19 | array_number[i] = rand.Intn(100) 20 | } 21 | 22 | fmt.Println("random number: ", array_number) 23 | 24 | for i := 0; i < array_length; i++ { 25 | toswap := i 26 | lowest := array_number[i] 27 | 28 | for j := i; j >= 0; j-- { 29 | 30 | if array_number[j] > lowest { 31 | array_number[j+1] = array_number[j] 32 | toswap = j 33 | } 34 | } 35 | 36 | if toswap != i { 37 | array_number[toswap] = lowest 38 | } 39 | } 40 | 41 | fmt.Println("sorted number: ", array_number) 42 | } 43 | -------------------------------------------------------------------------------- /go/sort/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | fmt.Printf("\t\tCode Geek\n\n") 10 | fmt.Println("Language: Go") 11 | fmt.Println("Category: Sorting") 12 | hr() 13 | 14 | // sorting algoritwhm 15 | measure(sort_in_one_loop) 16 | hr() 17 | measure(bubble_sort) 18 | hr() 19 | measure(selection_sort) 20 | hr() 21 | measure(insertion_sort) 22 | hr() 23 | } 24 | 25 | // hr function use to print dividing line 26 | func hr() { 27 | fmt.Printf("============================================================\n") 28 | } 29 | 30 | // measure function use to measure elapsed time to run a function 31 | func measure(f func()) { 32 | start_time := time.Now() 33 | f() 34 | elapsed := float64(time.Since(start_time).Nanoseconds()) / 1000000 35 | fmt.Println("elapsed time: ", elapsed, "ms") 36 | } 37 | -------------------------------------------------------------------------------- /go/sort/selection-sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | ) 7 | 8 | // In computer science, selection sort is a sorting algorithm, specifically an in-place comparison sort. 9 | // It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. 10 | // Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, 11 | // particularly where auxiliary memory is limited. 12 | // 13 | // The algorithm divides the input list into two parts: the sublist of items already sorted, 14 | // which is built up from left to right at the front (left) of the list, 15 | // and the sublist of items remaining to be sorted that occupy the rest of the list. 16 | // Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. 17 | // The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, 18 | // exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), 19 | // and moving the sublist boundaries one element to the right. 20 | // https://en.wikipedia.org/wiki/Selection_sort 21 | // Level: Easy 22 | func selection_sort() { 23 | fmt.Println("selection sort") 24 | 25 | var array_number [50]int 26 | array_length := len(array_number) 27 | 28 | for i := 0; i < array_length; i++ { 29 | array_number[i] = rand.Intn(100) 30 | } 31 | 32 | fmt.Println("random number: ", array_number) 33 | 34 | for i := 0; i < array_length; i++ { 35 | toswap := i 36 | lowest := array_number[i] 37 | 38 | for j := i; j < array_length; j++ { 39 | 40 | if array_number[j] < lowest { 41 | lowest = array_number[j] 42 | toswap = j 43 | } 44 | } 45 | 46 | if toswap != i { 47 | temp := array_number[i] 48 | array_number[i] = lowest 49 | array_number[toswap] = temp 50 | } 51 | } 52 | 53 | fmt.Println("sorted number: ", array_number) 54 | } 55 | -------------------------------------------------------------------------------- /go/sort/sort-in-one-loop.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math/rand" 6 | ) 7 | 8 | // Here is a simple trick to do an array sort in just one loop. 9 | // Level: Easy 10 | func sort_in_one_loop() { 11 | fmt.Println("sort in one loop") 12 | 13 | var array_number [50]int 14 | array_length := len(array_number) 15 | 16 | for i := 0; i < array_length; i++ { 17 | array_number[i] = rand.Intn(100) 18 | } 19 | 20 | fmt.Println("random number: ", array_number) 21 | 22 | i := 0 23 | for i < array_length { 24 | 25 | if i != array_length-1 && 26 | i >= 0 && 27 | array_number[i] > array_number[i+1] { 28 | x := array_number[i] 29 | array_number[i] = array_number[i+1] 30 | array_number[i+1] = x 31 | i -= 2 32 | } 33 | 34 | i++ 35 | } 36 | 37 | fmt.Println("sorted number: ", array_number) 38 | } 39 | -------------------------------------------------------------------------------- /go/string/common_subsequence.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | /* 6 | The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common 7 | to all sequences in a set of sequences (often just two sequences). 8 | 9 | source: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem 10 | 11 | You are given 2 string and you should return the longest common subsequence word of it. 12 | Level: Medium 13 | */ 14 | func common_subsequence() { 15 | fmt.Println("common subsequence") 16 | 17 | var string_1 string 18 | var string_2 string 19 | var memoize map[int]map[int]string 20 | 21 | string_1 = "VAIXBKNDFLDMORKQUBJP" 22 | string_2 = "XGRKPROQISGKLNMWPUQO" 23 | memoize = map[int]map[int]string{} 24 | fmt.Printf("first string: %s, second string: %s.\nThe common subsequence is: %s\n\n", 25 | string_1, string_2, common_subsequence_(string_1, string_2, 0, 0, memoize)) 26 | 27 | string_1 = "apple" 28 | string_2 = "banana" 29 | memoize = map[int]map[int]string{} 30 | fmt.Printf("first string: %s, second string: %s.\nThe common subsequence is: %s\n\n", 31 | string_1, string_2, common_subsequence_(string_1, string_2, 0, 0, memoize)) 32 | 33 | string_1 = "velociraptor" 34 | string_2 = "marsupilami" 35 | memoize = map[int]map[int]string{} 36 | fmt.Printf("first string: %s, second string: %s.\nThe common subsequence is: %s\n\n", 37 | string_1, string_2, common_subsequence_(string_1, string_2, 0, 0, memoize)) 38 | 39 | string_1 = "i love you" 40 | string_2 = "i hate you" 41 | memoize = map[int]map[int]string{} 42 | fmt.Printf("first string: %s, second string: %s.\nThe common subsequence is: %s\n\n", 43 | string_1, string_2, common_subsequence_(string_1, string_2, 0, 0, memoize)) 44 | } 45 | 46 | func common_subsequence_(s_1, s_2 string, x, y int, memoize map[int]map[int]string) (result string) { 47 | if len(memoize[x][y]) > 0 { 48 | return memoize[x][y] 49 | } 50 | 51 | if x == len(s_1) || y == len(s_2) { 52 | return "" 53 | } else if s_1[x] == s_2[y] { 54 | if len(memoize[x]) == 0 { 55 | memoize[x] = map[int]string{} 56 | } 57 | tmp := result + string(s_1[x]) + common_subsequence_(s_1, s_2, x+1, y+1, memoize) 58 | memoize[x][y] = tmp 59 | return tmp 60 | } else if s_1[x] != s_2[y] { 61 | r1 := result + common_subsequence_(s_1, s_2, x, y+1, memoize) 62 | r2 := result + common_subsequence_(s_1, s_2, x+1, y, memoize) 63 | if len(r1) > len(r2) { 64 | result = r1 65 | } else { 66 | result = r2 67 | } 68 | 69 | if len(memoize[x]) == 0 { 70 | memoize[x] = map[int]string{} 71 | } 72 | memoize[x][y] = result 73 | return result 74 | } 75 | 76 | return result 77 | } 78 | -------------------------------------------------------------------------------- /go/string/longest_common_subsequence.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | /* 6 | You are given 2 string and you should return the longest common subsequence 7 | Level: Medium 8 | */ 9 | func longest_common_subsequence() { 10 | fmt.Println("longest common subsequence") 11 | 12 | var string_1 string 13 | var string_2 string 14 | var memoize = map[int]map[int]int{} 15 | 16 | string_1 = "banana" 17 | string_2 = "ohana" 18 | memoize = map[int]map[int]int{} 19 | fmt.Printf("first string: %s, second string: %s.\nThe longest common subsequence length is: %d\n\n", 20 | string_1, string_2, lcs(string_1, string_2, len(string_1), len(string_2), memoize)) 21 | 22 | string_1 = "apple" 23 | string_2 = "apple" 24 | memoize = map[int]map[int]int{} 25 | fmt.Printf("first string: %s, second string: %s.\nThe longest common subsequence length is: %d\n\n", 26 | string_1, string_2, lcs(string_1, string_2, len(string_1), len(string_2), memoize)) 27 | 28 | string_1 = "velociraptor" 29 | string_2 = "marsupilami" 30 | memoize = map[int]map[int]int{} 31 | fmt.Printf("first string: %s, second string: %s.\nThe longest common subsequence length is: %d\n\n", 32 | string_1, string_2, lcs(string_1, string_2, len(string_1), len(string_2), memoize)) 33 | 34 | string_1 = "i love you" 35 | string_2 = "i hate you" 36 | memoize = map[int]map[int]int{} 37 | fmt.Printf("first string: %s, second string: %s.\nThe longest common subsequence length is: %d\n\n", 38 | string_1, string_2, lcs(string_1, string_2, len(string_1), len(string_2), memoize)) 39 | 40 | } 41 | 42 | func lcs(x, y string, i, j int, memoize map[int]map[int]int) int { 43 | if memoize[i][j] != 0 { 44 | return memoize[i][j] 45 | } 46 | 47 | result := 1 48 | 49 | if i == 0 || j == 0 { 50 | return 0 51 | } else if x[i-1] == y[j-1] { 52 | result = 1 + lcs(x, y, i-1, j-1, memoize) 53 | } else if x[i-1] != y[j-1] { 54 | res1 := lcs(x, y, i-1, j, memoize) 55 | res2 := lcs(x, y, i, j-1, memoize) 56 | 57 | if res1 > res2 { 58 | result = res1 59 | } else { 60 | result = res2 61 | } 62 | } 63 | 64 | if len(memoize[i]) == 0 { 65 | memoize[i] = map[int]int{} 66 | } 67 | memoize[i][j] = result 68 | return result 69 | } 70 | -------------------------------------------------------------------------------- /go/string/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | fmt.Printf("\t\tCode Geek\n\n") 10 | fmt.Println("Language: Go") 11 | fmt.Println("Category: String") 12 | hr() 13 | 14 | measure(reverse_string) 15 | hr() 16 | measure(subsequence) 17 | hr() 18 | measure(longest_common_subsequence) 19 | hr() 20 | measure(common_subsequence) 21 | hr() 22 | } 23 | 24 | // hr function use to print dividing line 25 | func hr() { 26 | fmt.Printf("============================================================\n") 27 | } 28 | 29 | // measure function use to measure elapsed time to run a function 30 | func measure(f func()) { 31 | start_time := time.Now() 32 | f() 33 | elapsed := float64(time.Since(start_time).Nanoseconds()) / 1000000 34 | fmt.Println("elapsed time: ", elapsed, "ms") 35 | } 36 | -------------------------------------------------------------------------------- /go/string/reverse_string.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | /* 9 | You are given a sets of string and your job is to reverse them. 10 | 11 | Level: Easy 12 | */ 13 | func reverse_string() { 14 | fmt.Println("reverse string") 15 | 16 | var to_be_reverse string = "foo bar" 17 | var reversed_string string 18 | 19 | reversed_string = reverse_string_(to_be_reverse) 20 | fmt.Println("normal string:", to_be_reverse, ", reversed string:", reversed_string) 21 | 22 | to_be_reverse = "when i was a young boy, my father took me into the city" 23 | reversed_string = reverse_string_(to_be_reverse) 24 | fmt.Println("normal string:", to_be_reverse, ", reversed string:", reversed_string) 25 | 26 | to_be_reverse = "and i'm still breathing, i'm still breathing on my own" 27 | reversed_string = reverse_string_(to_be_reverse) 28 | fmt.Println("normal string:", to_be_reverse, ", reversed string:", reversed_string) 29 | 30 | to_be_reverse = "maybe i'm not the one that make you smile, but i guarantee i'm the one who make you feel sexy" 31 | reversed_string = reverse_string_(to_be_reverse) 32 | fmt.Println("normal string:", to_be_reverse, ", reversed string:", reversed_string) 33 | 34 | to_be_reverse = "lorem ipsum dolor sit amet" 35 | reversed_string = reverse_string_(to_be_reverse) 36 | fmt.Println("normal string:", to_be_reverse, ", reversed string:", reversed_string) 37 | } 38 | 39 | func reverse_string_(to_be_reverse string) string { 40 | string_array := strings.Split(to_be_reverse, "") 41 | string_length := len(string_array) 42 | 43 | start := 0 44 | last := string_length - 1 45 | for start < last { 46 | x := string_array[start] 47 | string_array[start] = string_array[last] 48 | string_array[last] = x 49 | 50 | start++ 51 | last-- 52 | } 53 | 54 | return strings.Join(string_array, "") 55 | } 56 | -------------------------------------------------------------------------------- /go/string/subsequence.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | /* 8 | You are given task to print a possible subsequence of word, so what is subsequence? 9 | 10 | In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some or 11 | no elements without changing the order of the remaining elements. For example, the sequence {A,B,D} 12 | is a subsequence of {A,B,C,D,E,F} obtained after removal of elements C, E, and F. 13 | The relation of one sequence being the subsequence of another is a preorder. 14 | 15 | The subsequence should not be confused with substring {A,B,C,D} which can be derived from the above string {A,B,C,D,E,F} 16 | by deleting substring {E,F}. The substring is a refinement of the subsequence. 17 | 18 | The list of all subsequences for the word "apple" would be 19 | "a, ap, al, ae, app, apl, ape, ale, appl, appe, aple, apple, p, pp, pl, pe, ppl, ppe, ple, pple, l, le, e". 20 | 21 | source: https://en.wikipedia.org/wiki/Subsequence 22 | 23 | Level: Medium 24 | */ 25 | func subsequence() { 26 | fmt.Println("subsequence") 27 | 28 | var text string 29 | var empty_string_array []string 30 | var in_array = map[string]bool{} 31 | 32 | text = "apple" 33 | fmt.Printf("subsequence of \"%s\" is:\n", text) 34 | fmt.Printf("%s\n\n", subsequence_(text, len(text), empty_string_array, in_array)) 35 | in_array = map[string]bool{} 36 | 37 | text = "i love you" 38 | fmt.Printf("subsequence of \"%s\" is:\n", text) 39 | fmt.Printf("%s\n\n", subsequence_(text, len(text), empty_string_array, in_array)) 40 | in_array = map[string]bool{} 41 | } 42 | 43 | // Function subsequence receive 4 parameter 44 | // var text string => actual text 45 | // var index integer => text length or start index to do a subsequence function 46 | // var subsequence []string => list of possible subsequence 47 | // var in_array map[string]bool => list of memoized text to optimize the function 48 | func subsequence_(text string, index int, subsequence []string, in_array map[string]bool) []string { 49 | // If its a blank text or index bigger than text length, then it should be return instead. 50 | if text == "" || index > len(text) || in_array[text] { 51 | return subsequence 52 | } 53 | 54 | // Add text to subsequence array and memoize the text 55 | subsequence = append(subsequence, text) 56 | in_array[text] = true 57 | 58 | for i := 1; i <= len(text)-1; i++ { 59 | for j := i + 2; j <= len(text); j++ { 60 | combined_text := text[:i] + text[j:] // Combine first char and third char and so on 61 | if in_array[combined_text] { // If combined text already in subsequence array then ignore it 62 | continue 63 | } 64 | subsequence = subsequence_(combined_text, len(combined_text), subsequence, in_array) // Call subsequence function for combined text 65 | } 66 | } 67 | 68 | for index >= 0 { 69 | i := index // Store index value in i before go to next request 70 | index-- 71 | if in_array[text[i:]] == false { 72 | subsequence = subsequence_(text[i:], index, subsequence, in_array) // Call subsequence function for text after i 73 | } else if in_array[text[:i]] == false { 74 | subsequence = subsequence_(text[:i], index, subsequence, in_array) // Call subsequence function for text before i 75 | } 76 | } 77 | 78 | return subsequence 79 | } 80 | -------------------------------------------------------------------------------- /java/recursion/TriangleRecursion.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * Recursion occurs when a thing is defined in terms of itself or of its type. 5 | * Recursion is used in variety of disciplines ranging from linguistics to login. 6 | * The most common application of recursion is in mathematics and computer science, 7 | * where a function being defined is applied within its own definition. While 8 | * this apparently defines an infinite number of instances (function values), 9 | * it is often done in such a way that no loop or infinite chain of references 10 | * can occur. 11 | * https://en.wikipedia.org/wiki/Recursion 12 | * Level: Medium 13 | */ 14 | class TriangleRecursion { 15 | public static void main(String[] args) { 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.print("Input number: "); 18 | try { 19 | int inputNumber = scanner.nextInt(); 20 | printTriangle(inputNumber, inputNumber); 21 | } catch (Exception e) { 22 | e.printStackTrace(); 23 | System.out.println("Invalid data"); 24 | } 25 | } 26 | 27 | static void printTriangle(int initialValue, int finalValue) { 28 | if (initialValue < 1) { 29 | return; 30 | } else { 31 | for (int a = initialValue; a <= finalValue; a++) { 32 | System.out.print("*"); 33 | } 34 | System.out.println(); 35 | printTriangle(--initialValue, finalValue ); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /java/searching/BinarySearching.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | /** 5 | * In computer science, binary search, also known as half-interval search, 6 | * logarithmic search, or binary chop, is a search algorithm that finds 7 | * the position of a target value within a sorted array. Binary search 8 | * compares the target value to the middle element of the array. If 9 | * they are not equal, the half in which the target cannot lie is eliminated 10 | * and the search continues on the remaining half, again taking the middle 11 | * element to compare to the target value, and repeating this until the 12 | * target value is found. If the search ends with the remaining half being 13 | * empty, the target is not in the array. Even though the idea is simple, 14 | * implementing binary search correctly requires attention to some subtleties 15 | * about its exit conditions and midpoint calculation, particularly if the 16 | * values in the array are not all of the whole numbers in the range. 17 | * 18 | * Binary search runs in logarithmic time in the worst case, making O(log n) 19 | * comparisons, where n is the number of elements in the array, the O is 20 | * Big O notation, and log is the logarithm. Binary search takes constant 21 | * (O(1)) space, meaning that the space taken by the algorithm is the same 22 | * for any number of elements in the array. Binary search is faster than 23 | * linear search except for small arrays, but the array must be sorted first. 24 | * Although specialized data structures designed for fast searching, such as 25 | * hash tables, can be searched more efficiently, binary search applies to 26 | * a wider range of problems. 27 | * 28 | * There are numerous variations of binary search. In particular, fractional 29 | * cascading speeds up binary searches for the same value in multiple arrays. 30 | * Fractional cascading efficiently solves a number of search problems in 31 | * computational geometry and in numerous other fields. Exponential search 32 | * extends binary search to unbounded lists. The binary search tree and B-tree 33 | * data structures are based on binary search. 34 | * https://en.wikipedia.org/wiki/Binary_search_algorithm 35 | * Level: Medium 36 | */ 37 | class BinarySearching { 38 | public static void main(String[] args) { 39 | Scanner scanner = new Scanner(System.in); 40 | try { 41 | // Input 42 | System.out.print("Input jumlah data: "); 43 | int amountOfData = scanner.nextInt(); 44 | System.out.print("Input item data (setiap item dipisahkan dengan spasi): "); 45 | int[] itemData = new int[amountOfData]; 46 | for (int a = 0; a < amountOfData; a++) { 47 | itemData[a] = Integer.parseInt(scanner.next()); 48 | } 49 | System.out.print("Input item data yang dicari: "); 50 | int itemSearch = scanner.nextInt(); 51 | 52 | // Process 53 | // Sorting 54 | for (int a = 0; a < amountOfData - 1; a++) { 55 | int itemA = itemData[a]; 56 | int indexLowest = a + 1; 57 | int lowest = itemData[indexLowest]; 58 | for (int b = a + 1; b < amountOfData; b++) { 59 | int itemB = itemData[b]; 60 | if (itemB < indexLowest) { 61 | indexLowest = b; 62 | lowest = itemB; 63 | } 64 | } 65 | if (lowest < itemA) { 66 | itemData[a] = lowest; 67 | itemData[indexLowest] = itemA; 68 | } 69 | } 70 | 71 | // Binary searching 72 | int count = 0; 73 | boolean isFound = false; 74 | int initialIndex = 0; 75 | int finalIndex = amountOfData - 1; 76 | int tempValue = 0; 77 | while (!isFound) { 78 | int middleIndex = (initialIndex + finalIndex) / 2; 79 | int value = itemData[middleIndex]; 80 | if (value == itemSearch) { 81 | isFound = true; 82 | break; 83 | } else if (value < itemSearch) { 84 | initialIndex = middleIndex + 1; 85 | } else if (value > itemSearch) { 86 | finalIndex = middleIndex - 1; 87 | } 88 | if (value == tempValue) { 89 | count += 1; 90 | } else { 91 | tempValue = value; 92 | count = 0; 93 | } 94 | if (count == 3) { 95 | break; 96 | } 97 | } 98 | if (isFound) { 99 | System.out.println("Output: data yang dicari ditemukan"); 100 | } else { 101 | System.out.println("Output: data yang dicari tidak ditemukan"); 102 | } 103 | } catch (Exception e) { 104 | e.printStackTrace(); 105 | System.out.println("Invalid data"); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /java/searching/SequentialSearching.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * In computer science, linear search or sequential search is a method 5 | * for finding an element within a list. It sequentially checks each 6 | * element of the list until a match is found or the whole list has 7 | * been searched. 8 | * 9 | * Linear search runs in at worst linear time and makes at most 10 | * n comparisons, where n is the length of the list. If each element 11 | * is equally likely to be searched, then linear search has an average 12 | * case of n/2 comparisons, but the average case can be affected if 13 | * the search probabilities for each element vary. Linear search is 14 | * rarely practical because other search algorithms and schemes, 15 | * such as the binary search algorithm and hash tables, allow 16 | * significantly faster searching for all but short lists. 17 | * https://en.wikipedia.org/wiki/linear_search 18 | * Level: Easy 19 | */ 20 | class SequentialSearching { 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | try { 24 | // Input 25 | System.out.print("Input jumlah data: "); 26 | int jumlahData = scanner.nextInt(); 27 | System.out.print("Input item data (setiap item dipisahkan dengan spasi): "); 28 | int[] itemData = new int[jumlahData]; 29 | for (int a = 0; a < jumlahData; a++) { 30 | itemData[a] = Integer.parseInt(scanner.next()); 31 | } 32 | System.out.print("Input item data yang dicari: "); 33 | int dataCari = scanner.nextInt(); 34 | 35 | // Process 36 | boolean isFound = false; 37 | for (int item : itemData) { 38 | if (item == dataCari) { 39 | System.out.println("Output: data yang dicari ditemukan"); 40 | isFound = true; 41 | break; 42 | } 43 | } 44 | if (!isFound) { 45 | System.out.println("Output: data yang dicari tidak ditemukan"); 46 | } 47 | } catch (Exception e) { 48 | e.printStackTrace(); 49 | System.out.println("Invalid data"); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /java/sort/BubbleSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | /** 5 | * Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly 6 | * steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. 7 | * The pass through the list is repeated until the list is sorted. The algorithm, which is a comparison 8 | * sort, is named for the way smaller or larger elements "bubble" to the top of the list. Although the 9 | * algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. 10 | * Bubble sort can be practical if the input is mostly sorted order with some out-of-order. 11 | * https://en.wikipedia.org/wiki/Bubble_sort 12 | * Level: Easy 13 | */ 14 | class BubbleSort { 15 | public static void main(String[] args) { 16 | Scanner scanner = new Scanner(System.in); 17 | try { 18 | // Input 19 | System.out.print("Input jumlah data: "); 20 | int jumlahData = scanner.nextInt(); 21 | System.out.print("Input item data (setiap item dipisahkan dengan spasi): "); 22 | int[] itemData = new int[jumlahData]; 23 | for (int a = 0; a < jumlahData; a++) { 24 | itemData[a] = Integer.parseInt(scanner.next()); 25 | } 26 | 27 | // Process 28 | for (int a = 1; a < jumlahData; a++) { 29 | for (int b = 0; b < jumlahData - a; b++) { 30 | int itemA = itemData[b]; 31 | int itemB = itemData[b + 1]; 32 | if (itemA > itemB) { 33 | int temp = itemA; 34 | itemA = itemB; 35 | itemB = temp; 36 | itemData[b] = itemA; 37 | itemData[b + 1] = itemB; 38 | } 39 | } 40 | } 41 | 42 | // Output 43 | System.out.println("output: " + Arrays.toString(itemData)); 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | System.out.println("Invalid data"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /java/sort/InsertionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | /** 5 | * Insertion sort is a simple sorting algorithm that builds 6 | * the final sorted array (or list) one item at time. It is 7 | * much less efficient on large lists than more advanced 8 | * algorithms such as quicksort, heapsort, or merge sort. 9 | * https://en.wikipedia.org/wiki/Insertion_sort 10 | * Level: Medium 11 | */ 12 | class InsertionSort { 13 | public static void main(String[] args) { 14 | Scanner scanner = new Scanner(System.in); 15 | try { 16 | // Input 17 | System.out.print("Enter the amount of data: "); 18 | int amountData = scanner.nextInt(); 19 | int[] items = new int[amountData]; 20 | System.out.print("Input data (separated by spaces): "); 21 | for (int a = 0; a < amountData; a++) { 22 | items[a] = Integer.parseInt(scanner.next()); 23 | } 24 | 25 | // Process 26 | for (int a = 1; a < amountData; a++) { 27 | for (int b = a; b > 0; b--) { 28 | int itemA = items[b - 1]; 29 | int itemB = items[b]; 30 | if (itemA > itemB) { 31 | int temp = itemA; 32 | itemA = itemB; 33 | itemB = temp; 34 | items[b - 1] = itemA; 35 | items[b] = itemB; 36 | } else { 37 | break; 38 | } 39 | } 40 | } 41 | 42 | // Output 43 | System.out.println("Output: " + Arrays.toString(items)); 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | System.out.println("Invalid data"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /java/sort/SelectionSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | 4 | /** 5 | * In computer science, selection sort is a sorting algorithm, 6 | * specially an in-place-comparison sort. It has 0(n2) time complexity 7 | * making it inefficient on large lists, and generally performs worse 8 | * than the similar insertion sort. Selection sort is noted for its 9 | * simplicity, and it has performance advantages over more complicated 10 | * algorithms in certain situations, particularly where auxiliary memory 11 | * is limited. 12 | * 13 | * The algorithm divides the input list two parts: the sublist of items 14 | * already sorted, which is built up from left to right at the front (left) 15 | * of the list, and the sublist of items remaining to be sorted that occupy 16 | * the rest of the list. Initially, the sorted sublist is empty and the 17 | * unsorted sublist is the entire input list. The algorithm proceeds by 18 | * finding the smallest (or largest, depending on sorting order) element 19 | * in the unsorted sublist, exchanging (swapping) it with the leftmost 20 | * unsorted element (putting it in sorted order), and moving the sublist 21 | * boundaries one element to the right. 22 | * https://en.wikipedia.org/wiki/Selection_sort 23 | * Level: Easy 24 | */ 25 | class SelectionSort { 26 | public static void main(String[] args) { 27 | Scanner scanner = new Scanner(System.in); 28 | try { 29 | // Input 30 | System.out.print("Input jumlah data: "); 31 | int jumlahData = scanner.nextInt(); 32 | int[] itemData = new int[jumlahData]; 33 | System.out.print("Input item data (setiap item dipisahkan dengan spasi): "); 34 | for (int a = 0; a < jumlahData; a++) { 35 | itemData[a] = Integer.parseInt(scanner.next()); 36 | } 37 | 38 | // Process 39 | for (int a = 0; a < jumlahData - 1; a++) { 40 | int itemA = itemData[a]; 41 | int indexLowest = a + 1; 42 | int lowest = itemData[indexLowest]; 43 | for (int b = a + 1; b < jumlahData; b++) { 44 | int itemB = itemData[b]; 45 | if (lowest > itemB) { 46 | lowest = itemB; 47 | indexLowest = b; 48 | } 49 | } 50 | if (lowest < itemA) { 51 | itemData[a] = lowest; 52 | itemData[indexLowest] = itemA; 53 | } 54 | } 55 | 56 | // Output 57 | System.out.println("Output: " + Arrays.toString(itemData)); 58 | } catch (Exception e) { 59 | e.printStackTrace(); 60 | System.out.println("Invalid data"); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /python/dictionary/Dynamically_Importing_To_Dictionary.py: -------------------------------------------------------------------------------- 1 | """ 2 | :: BAHASA 3 | Disini terdapat kasus dimana anda harus melakukan import module secara dinamis dan lalu mendapatkan 4 | property yang ada didalam, setelah itu anda harus merubah import tersebut menjadi sebuah dictionary 5 | yang dimana didalamnya terdapat sebuah list yang berisikan variable yang telah diolah menjadi key 6 | dan value. 7 | ===================================================================================================== 8 | :: ENGLISH 9 | In this case you will be doing import module dynamically and get the property inside it, the you must 10 | change the import into a dictionary where inside the dictionary there's a list contain variable that 11 | already changed into key and value 12 | 13 | :: HOW TO USE 14 | $ python Dynamically_Importing_To_Dictionary.py 'your_python_module' 'your_variable', 'your_variable' ... 15 | for example: 16 | $ python Dynamically_Importing_To_Dictionary.py movies iron_man superman 17 | """ 18 | 19 | import sys, imp 20 | 21 | 22 | my_find = sys.argv[1] 23 | my_dict = {my_find: []} 24 | my_import = 'sample/%s.py' % my_find 25 | my_data = sys.argv[2:] 26 | 27 | for i in my_data: 28 | src = imp.load_source(i, my_import) 29 | my_dict[my_find].append({i : getattr(src, i)}) 30 | 31 | print(my_dict) 32 | -------------------------------------------------------------------------------- /python/dictionary/Searching_Dictionary_Inside_List.py: -------------------------------------------------------------------------------- 1 | """ 2 | :: BAHASA 3 | Dibawah ini terdapat kumpulan dictionary yang ada didalam list, akan tetapi tiap dictionary 4 | memiliki key yang sama tetapi value-nya berbeda. Kasusnya adalah bagaimana caranya untuk mendapatkan 5 | baris dictionary yang tepat hanya dengan menggunakan satu kata kunci dari key dan value yang berada 6 | didalam dictionary. 7 | ===================================================================================================== 8 | :: ENGLISH 9 | There's bunch of dictionary inside list, however each dictionary has same key but different value. 10 | In this case how to find the correct dictionary with just single keyword based on index and value 11 | inside the dictionary. 12 | 13 | :: HOW TO USE 14 | $ python Searching_Dictionary_Inside_List.py 'your_key' 'your_value' 15 | for example: 16 | $ python Searching_Dictionary_Inside_List.py name 'John Doe' 17 | """ 18 | 19 | import sys 20 | 21 | user = [ 22 | {'name': 'John Doe', 'city': 'California', 'country': 'US'}, 23 | {'name': 'Harry Brian', 'city': 'London', 'country': 'UK'}, 24 | {'name': 'Jared Argus', 'city': 'Miami', 'country': 'US'}, 25 | {'name': 'Brian Dom', 'city': 'Mexico City', 'country': 'Mexico'}, 26 | {'name': 'Harry Brian', 'city': 'Jakarta', 'country': 'Indonesia'}, 27 | {'name': 'Agus Suparjo', 'city': 'Malang', 'country': 'Indonesia'}, 28 | {'name': 'Jane Doe', 'city': 'Kansas', 'country': 'US'}, 29 | {'name': 'Jerome Dominique', 'city': 'California', 'country': 'US'}, 30 | {'name': 'Tyrone', 'city': 'Detroit', 'country': 'US'}, 31 | {'name': 'James Bond', 'city': 'London', 'country': 'UK'} 32 | ] 33 | 34 | key = sys.argv[1] 35 | value = sys.argv[2] 36 | 37 | print(filter(lambda dct: dct[key] == value, user)) 38 | -------------------------------------------------------------------------------- /python/dictionary/sample/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insomnius/code-geek/0ebe21333db5eac5dd49ff61d77858fa701113bd/python/dictionary/sample/__init__.py -------------------------------------------------------------------------------- /python/dictionary/sample/movies.py: -------------------------------------------------------------------------------- 1 | iron_man = { 2 | 'studio': 'marvel', 3 | 'main_character': 'robert downey jr', 4 | 'year': '2006' 5 | } 6 | 7 | superman = { 8 | 'studio': 'DC', 9 | 'main_character': 'hari canvil', 10 | 'year': 'unknown' 11 | } 12 | 13 | lock_up = { 14 | 'studio': 'Unknown', 15 | 'main_character': 'silvester stallone', 16 | 'year': '1992' 17 | } 18 | -------------------------------------------------------------------------------- /python/dictionary/sample/musics.py: -------------------------------------------------------------------------------- 1 | orbit = { 2 | 'artist': 'Thomas bergersen', 3 | 'album': 'single' 4 | } 5 | 6 | sun = { 7 | 'artist': 'Thomas bergersen', 8 | 'album': 'Sun' 9 | } 10 | 11 | compass = { 12 | 'artist': 'Two steps from hell and Meredth Soldvelt', 13 | 'album': 'Miracles' 14 | } 15 | 16 | miracles = { 17 | 'artist': 'Two steps from hell', 18 | 'album': 'Miracles' 19 | } 20 | --------------------------------------------------------------------------------