Repository: TheAlgorithms/Go
Branch: master
Commit: 5ba447ec5ff3
Files: 519
Total size: 876.1 KB
Directory structure:
gitextract_m9961ttm/
├── .github/
│ ├── CODEOWNERS
│ ├── ISSUE_TEMPLATE/
│ │ ├── bug_report.yml
│ │ ├── config.yml
│ │ ├── new_implementation.yml
│ │ └── other.yml
│ ├── PULL_REQUEST_TEMPLATE/
│ │ └── pull_request.md
│ ├── dependabot.yml
│ └── workflows/
│ ├── ci.yml
│ ├── citk.yml
│ ├── godocmd.yml
│ ├── stale.yml
│ └── upload_coverage_report.yml
├── .gitignore
├── .gitpod.dockerfile
├── .gitpod.yml
├── .golangci.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── STYLE.md
├── cache/
│ ├── lfu.go
│ ├── lfu_test.go
│ ├── lru.go
│ └── lru_test.go
├── checksum/
│ ├── crc8.go
│ ├── crc8_test.go
│ ├── luhn.go
│ └── luhn_test.go
├── cipher/
│ ├── caesar/
│ │ ├── caesar.go
│ │ └── caesar_test.go
│ ├── cipher_test.go
│ ├── diffiehellman/
│ │ ├── diffiehellmankeyexchange.go
│ │ └── diffiehellmankeyexchange_test.go
│ ├── doc.go
│ ├── dsa/
│ │ ├── dsa.go
│ │ └── dsa_test.go
│ ├── polybius/
│ │ ├── polybius.go
│ │ └── polybius_test.go
│ ├── railfence/
│ │ ├── railfence.go
│ │ └── railfence_test.go
│ ├── rot13/
│ │ ├── rot13.go
│ │ └── rot13_test.go
│ ├── rsa/
│ │ ├── rsa.go
│ │ ├── rsa2.go
│ │ ├── rsa2_test.go
│ │ └── rsa_test.go
│ ├── transposition/
│ │ ├── transposition.go
│ │ └── transposition_test.go
│ └── xor/
│ ├── xor.go
│ └── xor_test.go
├── compression/
│ ├── huffmancoding.go
│ ├── huffmancoding_test.go
│ ├── rlecoding.go
│ └── rlecoding_test.go
├── constraints/
│ └── constraints.go
├── conversion/
│ ├── base64.go
│ ├── base64_test.go
│ ├── binarytodecimal.go
│ ├── binarytodecimal_test.go
│ ├── conversion_test.go
│ ├── decimaltobinary.go
│ ├── decimaltobinary_test.go
│ ├── doc.go
│ ├── hexadecimaltobinary.go
│ ├── hexadecimaltobinary_test.go
│ ├── hexadecimaltodecimal.go
│ ├── hexadecimaltodecimal_test.go
│ ├── inttoroman.go
│ ├── inttoroman_test.go
│ ├── rgbhex.go
│ ├── rgbhex_test.go
│ ├── romantoint.go
│ └── romantoint_test.go
├── dynamic/
│ ├── abbreviation.go
│ ├── abbreviation_test.go
│ ├── binomialcoefficient.go
│ ├── binomialcoefficient_test.go
│ ├── burstballoons.go
│ ├── burstballoons_test.go
│ ├── catalan.go
│ ├── catalan_test.go
│ ├── coinchange.go
│ ├── coinchange_test.go
│ ├── dicethrow.go
│ ├── dicethrow_test.go
│ ├── doc.go
│ ├── dynamic_test.go
│ ├── editdistance.go
│ ├── editdistance_test.go
│ ├── eggdropping.go
│ ├── eggdropping_test.go
│ ├── fibonacci.go
│ ├── fibonacci_test.go
│ ├── interleavingstrings.go
│ ├── interleavingstrings_test.go
│ ├── knapsack.go
│ ├── knapsack_test.go
│ ├── longestarithmeticsubsequence.go
│ ├── longestarithmeticsubsequence_test.go
│ ├── longestcommonsubsequence.go
│ ├── longestcommonsubsequence_test.go
│ ├── longestincreasingsubsequence.go
│ ├── longestincreasingsubsequence_test.go
│ ├── longestincreasingsubsequencegreedy.go
│ ├── longestpalindromicsubsequence.go
│ ├── longestpalindromicsubsequence_test.go
│ ├── longestpalindromicsubstring.go
│ ├── longestpalindromicsubstring_test.go
│ ├── matrixmultiplication.go
│ ├── maxsubarraysum.go
│ ├── maxsubarraysum_test.go
│ ├── optimalbst.go
│ ├── optimalbst_test.go
│ ├── partitionproblem.go
│ ├── partitionproblem_test.go
│ ├── rodcutting.go
│ ├── rodcutting_test.go
│ ├── subsetsum.go
│ ├── subsetsum_test.go
│ ├── tilingproblem.go
│ ├── tilingproblem_test.go
│ ├── traprainwater.go
│ ├── traprainwater_test.go
│ ├── uniquepaths.go
│ ├── uniquepaths_test.go
│ ├── wildcardmatching.go
│ ├── wildcardmatching_test.go
│ ├── wordbreak.go
│ └── wordbreak_test.go
├── go.mod
├── go.sum
├── graph/
│ ├── articulationpoints.go
│ ├── articulationpoints_test.go
│ ├── bellmanford.go
│ ├── bellmanford_test.go
│ ├── breadthfirstsearch.go
│ ├── breadthfirstsearch_test.go
│ ├── coloring/
│ │ ├── backtracking.go
│ │ ├── backtracking_test.go
│ │ ├── bfs.go
│ │ ├── bfs_test.go
│ │ ├── bipartite.go
│ │ ├── bipartite_test.go
│ │ ├── doc.go
│ │ ├── graph.go
│ │ ├── graph_test.go
│ │ ├── greedy.go
│ │ └── greedy_test.go
│ ├── cycle.go
│ ├── cycle_test.go
│ ├── depthfirstsearch.go
│ ├── depthfirstsearch_test.go
│ ├── dijkstra.go
│ ├── dijkstra_test.go
│ ├── doc.go
│ ├── edmondkarp.go
│ ├── edmondkarp_test.go
│ ├── floydwarshall.go
│ ├── floydwarshall_test.go
│ ├── graph.go
│ ├── graph_test.go
│ ├── kahn.go
│ ├── kahn_test.go
│ ├── kosaraju.go
│ ├── kosaraju_test.go
│ ├── kruskal.go
│ ├── kruskal_test.go
│ ├── lowestcommonancestor.go
│ ├── lowestcommonancestor_test.go
│ ├── prim.go
│ ├── prim_test.go
│ ├── topological.go
│ ├── topological_test.go
│ ├── unionfind.go
│ └── unionfind_test.go
├── hashing/
│ ├── doc.go
│ ├── hashing_test.go
│ ├── md5/
│ │ ├── md5.go
│ │ └── md5_test.go
│ ├── sha1/
│ │ ├── sha1.go
│ │ └── sha1_test.go
│ └── sha256/
│ ├── sha256.go
│ └── sha256_test.go
├── math/
│ ├── abs.go
│ ├── abs_test.go
│ ├── aliquot_test.go
│ ├── aliquotsum.go
│ ├── armstrong/
│ │ ├── isarmstrong.go
│ │ └── isarmstrong_test.go
│ ├── binary/
│ │ ├── abs.go
│ │ ├── abs_test.go
│ │ ├── arithmeticmean.go
│ │ ├── arithmeticmean_test.go
│ │ ├── bitcounter.go
│ │ ├── bitcounter_test.go
│ │ ├── checkisnumberpoweroftwo.go
│ │ ├── checkisnumberpoweroftwo_test.go
│ │ ├── fast_inverse_sqrt.go
│ │ ├── logarithm.go
│ │ ├── logarithm_test.go
│ │ ├── rbc.go
│ │ ├── rbc_test.go
│ │ ├── reversebits.go
│ │ ├── reversebits_test.go
│ │ ├── sqrt.go
│ │ ├── sqrt_test.go
│ │ ├── xorsearch.go
│ │ └── xorsearch_test.go
│ ├── binomialcoefficient.go
│ ├── binomialcoefficient_test.go
│ ├── catalan/
│ │ ├── catalannumber.go
│ │ └── catalannumber_test.go
│ ├── checkisnumberpoweroftwo.go
│ ├── checkisnumberpoweroftwo_test.go
│ ├── cos.go
│ ├── cos_test.go
│ ├── doc.go
│ ├── eulertotient.go
│ ├── eulertotient_test.go
│ ├── factorial/
│ │ ├── factorial.go
│ │ └── factorial_test.go
│ ├── fibonacci/
│ │ ├── fibonacci.go
│ │ └── fibonacci_test.go
│ ├── gcd/
│ │ ├── extended.go
│ │ ├── extended_test.go
│ │ ├── extendedgcd.go
│ │ ├── extendedgcd_test.go
│ │ ├── extendedgcditerative.go
│ │ ├── gcd.go
│ │ ├── gcd_test.go
│ │ └── gcditerative.go
│ ├── geometry/
│ │ ├── distance.go
│ │ ├── distance_test.go
│ │ ├── straightlines.go
│ │ └── straightlines_test.go
│ ├── isautomorphic.go
│ ├── isautomorphic_test.go
│ ├── krishnamurthy.go
│ ├── krishnamurthy_test.go
│ ├── kthnumber.go
│ ├── kthnumber_test.go
│ ├── lcm/
│ │ ├── lcm.go
│ │ └── lcm_test.go
│ ├── lerp.go
│ ├── lerp_test.go
│ ├── liouville.go
│ ├── liouville_test.go
│ ├── math_test.go
│ ├── matrix/
│ │ ├── add.go
│ │ ├── add_test.go
│ │ ├── checkequal.go
│ │ ├── checkequal_test.go
│ │ ├── copy.go
│ │ ├── copy_test.go
│ │ ├── determinant.go
│ │ ├── determinant_test.go
│ │ ├── isvalid.go
│ │ ├── isvalid_test.go
│ │ ├── matchdimensions.go
│ │ ├── matchdimensions_test.go
│ │ ├── matrix.go
│ │ ├── matrix_test.go
│ │ ├── multiply.go
│ │ ├── multiply_test.go
│ │ ├── strassenmatrixmultiply.go
│ │ ├── strassenmatrixmultiply_test.go
│ │ ├── string.go
│ │ ├── string_test.go
│ │ ├── submatrix.go
│ │ ├── submatrix_test.go
│ │ ├── subtract.go
│ │ └── subtract_test.go
│ ├── max/
│ │ ├── bitwisemax.go
│ │ ├── bitwisemax_test.go
│ │ ├── max.go
│ │ └── max_test.go
│ ├── mean.go
│ ├── mean_test.go
│ ├── median.go
│ ├── median_test.go
│ ├── min/
│ │ ├── bitwisemin.go
│ │ ├── min.go
│ │ └── min_test.go
│ ├── mobius.go
│ ├── mobius_test.go
│ ├── mode.go
│ ├── mode_test.go
│ ├── modular/
│ │ ├── exponentiation.go
│ │ ├── exponentiation_test.go
│ │ ├── inverse.go
│ │ └── inverse_test.go
│ ├── moserdebruijnsequence/
│ │ ├── sequence.go
│ │ └── sequence_test.go
│ ├── pascal/
│ │ ├── pascaltriangle.go
│ │ └── pascaltriangle_test.go
│ ├── perfectnumber.go
│ ├── perfectnumber_test.go
│ ├── permutation/
│ │ ├── heaps.go
│ │ ├── heaps_test.go
│ │ ├── next_permutation.go
│ │ └── next_permutation_test.go
│ ├── pi/
│ │ ├── montecarlopi.go
│ │ ├── montecarlopi_test.go
│ │ ├── spigotpi.go
│ │ └── spigotpi_test.go
│ ├── pollard.go
│ ├── pollard_test.go
│ ├── power/
│ │ ├── fastexponent.go
│ │ ├── fastexponent_test.go
│ │ ├── powvialogarithm.go
│ │ └── powvialogarithm_test.go
│ ├── prime/
│ │ ├── millerrabintest.go
│ │ ├── prime_test.go
│ │ ├── primecheck.go
│ │ ├── primefactorization.go
│ │ ├── primefactorization_test.go
│ │ ├── sieve.go
│ │ ├── sieve2.go
│ │ ├── sieve2_test.go
│ │ ├── sieve_test.go
│ │ ├── twin.go
│ │ └── twin_test.go
│ ├── pronicnumber.go
│ ├── pronicnumber_test.go
│ ├── pythagoras/
│ │ ├── pythagoras.go
│ │ └── pythagoras_test.go
│ ├── sin.go
│ └── sin_test.go
├── other/
│ ├── doc.go
│ ├── maxsubarraysum/
│ │ ├── maxsubarraysum.go
│ │ └── maxsubarraysum_test.go
│ ├── nested/
│ │ ├── nestedbrackets.go
│ │ └── nestedbrackets_test.go
│ ├── other_test.go
│ └── password/
│ └── generator.go
├── project_euler/
│ ├── problem_1/
│ │ ├── problem1.go
│ │ └── problem1_test.go
│ ├── problem_10/
│ │ ├── problem10.go
│ │ └── problem10_test.go
│ ├── problem_11/
│ │ ├── problem11.go
│ │ └── problem11_test.go
│ ├── problem_12/
│ │ ├── problem12.go
│ │ └── problem12_test.go
│ ├── problem_13/
│ │ ├── problem13.go
│ │ └── problem13_test.go
│ ├── problem_14/
│ │ ├── problem14.go
│ │ └── problem14_test.go
│ ├── problem_15/
│ │ ├── problem15.go
│ │ └── problem15_test.go
│ ├── problem_16/
│ │ ├── problem16.go
│ │ └── problem16_test.go
│ ├── problem_17/
│ │ ├── input.go
│ │ ├── problem17.go
│ │ └── problem17_test.go
│ ├── problem_18/
│ │ ├── edge.go
│ │ ├── input.go
│ │ ├── leaf.go
│ │ ├── problem18.go
│ │ ├── problem18_test.go
│ │ ├── root.go
│ │ └── tree.go
│ ├── problem_19/
│ │ ├── problem19.go
│ │ └── problem19_test.go
│ ├── problem_2/
│ │ ├── problem2.go
│ │ └── problem2_test.go
│ ├── problem_20/
│ │ ├── problem20.go
│ │ └── problem20_test.go
│ ├── problem_3/
│ │ ├── problem3.go
│ │ └── problem3_test.go
│ ├── problem_4/
│ │ ├── problem4.go
│ │ └── problem4_test.go
│ ├── problem_5/
│ │ ├── problem5.go
│ │ └── problem5_test.go
│ ├── problem_6/
│ │ ├── problem6.go
│ │ └── problem6_test.go
│ ├── problem_7/
│ │ ├── problem7.go
│ │ └── problem7_test.go
│ ├── problem_8/
│ │ ├── problem8.go
│ │ └── problem8_test.go
│ └── problem_9/
│ ├── problem9.go
│ └── problem9_test.go
├── search/
│ ├── binary.go
│ ├── binary_test.go
│ ├── doc.go
│ ├── errors.go
│ ├── interpolation.go
│ ├── interpolation_test.go
│ ├── jump.go
│ ├── jump2.go
│ ├── jump2_test.go
│ ├── jump_test.go
│ ├── linear.go
│ ├── linear_test.go
│ ├── selectk.go
│ ├── selectk_test.go
│ ├── ternary.go
│ ├── ternary_test.go
│ └── testcases.go
├── sort/
│ ├── binaryinsertionsort.go
│ ├── bogosort.go
│ ├── bubblesort.go
│ ├── bucketsort.go
│ ├── circlesort.go
│ ├── cocktailsort.go
│ ├── combSort.go
│ ├── countingsort.go
│ ├── cyclesort.go
│ ├── doc.go
│ ├── exchangesort.go
│ ├── heapsort.go
│ ├── insertionsort.go
│ ├── mergesort.go
│ ├── oddevensort.go
│ ├── pancakesort.go
│ ├── patiencesort.go
│ ├── pigeonholesort.go
│ ├── quicksort.go
│ ├── radixsort.go
│ ├── selectionsort.go
│ ├── shellsort.go
│ ├── simplesort.go
│ ├── sorts_test.go
│ ├── stooge_sort.go
│ └── timsort.go
├── sqrt/
│ ├── sqrtdecomposition.go
│ └── sqrtdecomposition_test.go
├── strings/
│ ├── ahocorasick/
│ │ ├── advancedahocorasick.go
│ │ ├── advancedahocorasick_test.go
│ │ ├── ahocorasick.go
│ │ ├── ahocorasick_test.go
│ │ ├── patterns.txt
│ │ ├── shared.go
│ │ └── text.txt
│ ├── bom/
│ │ └── bom.go
│ ├── charoccurrence.go
│ ├── charoccurrence_test.go
│ ├── combination/
│ │ └── combination.go
│ ├── doc.go
│ ├── generateparentheses/
│ │ ├── generateparentheses.go
│ │ └── generateparentheses_test.go
│ ├── genetic/
│ │ ├── genetic.go
│ │ └── geneticalgorithm_test.go
│ ├── guid/
│ │ ├── guid.go
│ │ └── guid_test.go
│ ├── hamming/
│ │ ├── hammingdistance.go
│ │ └── hammingdistance_test.go
│ ├── horspool/
│ │ ├── horspool.go
│ │ └── horspool_test.go
│ ├── isisogram.go
│ ├── isisogram_test.go
│ ├── issubsequence.go
│ ├── issubsequence_test.go
│ ├── kmp/
│ │ ├── kmp.go
│ │ └── kmp_test.go
│ ├── levenshtein/
│ │ ├── levenshteindistance.go
│ │ └── levenshteindistance_test.go
│ ├── manacher/
│ │ ├── longestpalindrome.go
│ │ └── longestpalindrome_test.go
│ ├── palindrome/
│ │ ├── ispalindrome.go
│ │ └── ispalindrome_test.go
│ ├── pangram/
│ │ ├── ispangram.go
│ │ └── ispangram_test.go
│ ├── parenthesis/
│ │ ├── parenthesis.go
│ │ └── parenthesis_test.go
│ ├── search/
│ │ ├── boyermoore.go
│ │ ├── naive.go
│ │ └── patternsearch_test.go
│ └── strings_test.go
└── structure/
├── circularqueue/
│ ├── circularqueue_test.go
│ └── circularqueuearray.go
├── deque/
│ ├── deque.go
│ └── deque_test.go
├── doc.go
├── dynamicarray/
│ ├── dynamicarray.go
│ └── dynamicarray_test.go
├── fenwicktree/
│ ├── fenwicktree.go
│ └── fenwicktree_test.go
├── hashmap/
│ ├── hashmap.go
│ └── hashmap_test.go
├── heap/
│ ├── heap.go
│ └── heap_test.go
├── linkedlist/
│ ├── Readme.md
│ ├── cyclic.go
│ ├── cyclic_test.go
│ ├── doc.go
│ ├── doubly.go
│ ├── doubly_test.go
│ ├── shared.go
│ ├── singlylinkedlist.go
│ └── singlylinkedlist_test.go
├── queue/
│ ├── queue_test.go
│ ├── queuearray.go
│ ├── queuelinkedlist.go
│ └── queuelinklistwithlist.go
├── segmenttree/
│ ├── segmenttree.go
│ └── segmenttree_test.go
├── set/
│ ├── set.go
│ ├── set_test.go
│ └── setexample_test.go
├── stack/
│ ├── stack_test.go
│ ├── stackarray.go
│ ├── stacklinkedlist.go
│ └── stacklinkedlistwithlist.go
├── structure_test.go
├── tree/
│ ├── avl.go
│ ├── avl_test.go
│ ├── bstree.go
│ ├── bstree_test.go
│ ├── btree.go
│ ├── btree_test.go
│ ├── example_test.go
│ ├── rbtree.go
│ ├── rbtree_test.go
│ ├── tree.go
│ └── tree_test.go
└── trie/
├── trie.go
├── trie_bench_test.go
├── trie_test.go
└── trieexample_test.go
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/CODEOWNERS
================================================
# This is a comment.
# Each line is a file pattern followed by one or more owners.
# More details are here: https://help.github.com/articles/about-codeowners/
# The '*' pattern is global owners.
# Order is important. The last matching pattern has the most precedence.
* @raklaptudirm @yanglbme
================================================
FILE: .github/ISSUE_TEMPLATE/bug_report.yml
================================================
name: "Bug report"
description: "Create a report to help us improve"
title: "[BUG]"
labels: ["bug"]
body:
- type: textarea
id: description
attributes:
label: "Description"
description: "A clear and concise description of what the bug is."
validations:
required: true
- type: textarea
id: steps
attributes:
label: "Steps to reproduce"
description: "Steps to reproduce the behavior (if applicable)"
placeholder: |
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
validations:
required: false
- type: textarea
id: exceptedbhv
attributes:
label: "Excepted behavior"
description: "A clear and concise description of what you expected to happen."
validations:
required: true
- type: textarea
id: screenshots
attributes:
label: "Screenshots"
description: "If applicable, add screenshots to help explain your problem."
validations:
required: false
- type: textarea
id: context
attributes:
label: "Additional context"
description: "Add any other context about the problem here."
validations:
required: false
================================================
FILE: .github/ISSUE_TEMPLATE/config.yml
================================================
blank_issues_enabled: false
================================================
FILE: .github/ISSUE_TEMPLATE/new_implementation.yml
================================================
name: "New or optimize implementation"
description: "Propose an enhancement or a new algorithm implementation."
title: "[ENHANCEMENT]"
labels: ["enhancement"]
body:
- type: textarea
id: description
attributes:
label: What would you like to share?
description: Provide a clear and concise explanation of your issue.
validations:
required: true
- type: markdown
attributes:
value: |
For new implementations, please specify the name and problem statement for the algorithm.
For algorithm enhancements, specify what needs to be changed and why. For example:
- Adding tests.
- Optimizing logic.
- Refactoring the file and folders for better structure.
- type: textarea
id: issuedetails
attributes:
label: "Extra issue details"
description: "Write down all the issue/algorithm details mentioned above."
validations:
required: true
- type: textarea
id: extrainfo
attributes:
label: Additional information
description: Is there anything else we should know about this issue?
validations:
required: false
================================================
FILE: .github/ISSUE_TEMPLATE/other.yml
================================================
name: Other
description: Use this for any other issues. Do NOT create blank issues
title: "[OTHER]"
labels: ["awaiting triage"]
body:
- type: markdown
attributes:
value: "# Other issue"
- type: textarea
id: issuedescription
attributes:
label: What would you like to share?
description: Provide a clear and concise explanation of your issue.
validations:
required: true
- type: textarea
id: extrainfo
attributes:
label: Additional information
description: Is there anything else we should know about this issue?
validations:
required: false
================================================
FILE: .github/PULL_REQUEST_TEMPLATE/pull_request.md
================================================
#### Description of Change
#### Checklist
- [ ] Added description of change
- [ ] Added file name matches [File name guidelines](https://github.com/TheAlgorithms/Go/blob/master/CONTRIBUTING.md#New-File-Name-guidelines)
- [ ] Added tests and example, test must pass
- [ ] Added documentation so that the program is self-explanatory and educational - [GoDoc guidelines](https://blog.golang.org/godoc)
- [ ] Relevant documentation/comments is changed or added
- [ ] PR title follows semantic [commit guidelines](https://github.com/TheAlgorithms/Go/blob/master/CONTRIBUTING.md#Commit-Guidelines)
- [ ] Search previous suggestions before making a new one, as yours may be a duplicate.
- [ ] I acknowledge that all my contributions will be made under the project's license.
Notes:
================================================
FILE: .github/dependabot.yml
================================================
# Keep GitHub Actions up to date with GitHub's Dependabot...
# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem
version: 2
updates:
- package-ecosystem: github-actions
directory: /
groups:
github-actions:
patterns:
- "*" # Group all Actions updates into a single larger pull request
schedule:
interval: weekly
================================================
FILE: .github/workflows/ci.yml
================================================
# https://github.com/golangci/golangci-lint
name: Continuous Integration
on:
push:
# prevent duplication of tests with
# `pull_request` event
branches:
- master
pull_request:
jobs:
golang_lint_and_test:
name: Code style and tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '^1.18'
- name: Run Golang CI Lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
args: -E gofmt
- name: Run tests
run: go test ./...
codespell:
name: Check for spelling errors
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: codespell-project/actions-codespell@master
with:
ignore_words_list: "actualy,nwe"
skip: "go.mod,go.sum"
================================================
FILE: .github/workflows/citk.yml
================================================
# https://github.com/golangci/golangci-lint
name: CI tool kit
on:
pull_request:
jobs:
CITK:
name: Code style and tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "^1.18"
- name: Checkout branch
run: |
git fetch origin master:master
- name: Install citk tool
run: |
go install github.com/tjgurwara99/citk@latest
- name: Run citk tool
run: |
citk check -l go -b master
================================================
FILE: .github/workflows/godocmd.yml
================================================
name: Generate Documentation
on:
push:
branches:
- master
jobs:
generate_readme:
name: Markdown Generation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: '^1.18'
- name: Install GoDocMD
run: |
go install github.com/tjgurwara99/godocmd@v0.1.3
- name: Configure Github Action
run: |
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
- name: Update README.md file
run: |
godocmd -r -module ./ -w
- name: Commit changes if README.md is different
run: |
if [[ `git status --porcelain` ]]; then
git commit -am "Updated Documentation in README.md"
git push
else
echo "NO CHANGES DETECTED"
fi
================================================
FILE: .github/workflows/stale.yml
================================================
name: 'Close stale issues and PRs'
on:
schedule:
- cron: '0 0 * * *'
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v9
with:
stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.'
close-issue-message: 'This issue was closed because it has been stalled for 7 days with no activity.'
stale-pr-message: 'This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.'
close-pr-message: 'This PR was closed because it has been stalled for 7 days with no activity.'
exempt-issue-labels: 'dont-close'
exempt-pr-labels: 'dont-close'
days-before-stale: 30
days-before-close: 7
================================================
FILE: .github/workflows/upload_coverage_report.yml
================================================
---
name: upload_coverage_report
on:
workflow_dispatch:
push:
branches:
- master
pull_request:
env:
REPORT_NAME: "coverage.out"
jobs:
upload_coverage_report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '^1.18'
- name: Generate code coverage
run: |
go test -coverprofile="${{ env.REPORT_NAME }}" ./...
- name: Upload coverage to codecov (tokenless)
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
uses: codecov/codecov-action@v5
with:
files: "${{ env.REPORT_NAME }}"
fail_ci_if_error: true
- name: Upload coverage to codecov (with token)
if: "! github.event.pull_request.head.repo.fork "
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: "${{ env.REPORT_NAME }}"
fail_ci_if_error: true
...
================================================
FILE: .gitignore
================================================
.idea/
.vscode/
coverage.out
================================================
FILE: .gitpod.dockerfile
================================================
FROM gitpod/workspace-go
================================================
FILE: .gitpod.yml
================================================
---
image:
file: .gitpod.dockerfile
tasks:
- init: |
echo "Welcome to TheAlgorithms/Go"
================================================
FILE: .golangci.yml
================================================
run:
go: 1.19
================================================
FILE: CONTRIBUTING.md
================================================
# CONTRIBUTION GUIDELINES
## Before contributing
Welcome to [TheAlgorithms/Go](https://github.com/TheAlgorithms/Go)! Before submitting pull requests, please make sure that you have **read the whole guidelines**. If you have any doubts about this contribution guide, please open [an issue](https://github.com/TheAlgorithms/Go/issues/new/choose) or ask in our [Discord server](https://the-algorithms.com/discord/) / [Gitter](https://gitter.im/TheAlgorithms), and clearly state your concerns.
## Contributing
### Maintainers
---
Please check the [`CODEOWNERS`](https://github.com/TheAlgorithms/Go/blob/master/.github/CODEOWNERS) file for a list of repository maintainers.
### Contributor
---
Being a contributor at The Algorithms, we request you to follow the points mentioned below:
- You did your own work.
- No plagiarism is allowed. Any plagiarized work will not be merged.
- Your work will be distributed under the [MIT License](https://github.com/TheAlgorithms/Go/blob/master/LICENSE) once your pull request has been merged.
- Please follow the repository guidelines and standards mentioned below.
#### New implementation
- New implementations are welcome!
- You can add new algorithms or data structures that are **not present in the repository** or that can **improve** the old implementations (**documentation**, **improving test cases**, **removing bugs**, or in any other reasonable sense)
#### Issues
- Please avoid opening issues asking to be "assigned” to a particular algorithm. This merely creates unnecessary noise for maintainers. Instead, please submit your implementation in a pull request, and it will be evaluated by project maintainers.
### Making Changes
---
#### Code
- Please use the directory structure of the repository.
- Make sure the file extensions should be `*.go`.
- Use meaningful variable names.
- Use standard library inside your code and avoid to import packages from other repositories
- If an implementation of the algorithm already exists, please refer to the [filename section below](#new-file-name-guidelines).
- You can suggest reasonable changes to existing algorithms.
- Strictly use snake_case (underscore_separated) in filenames.
- If you have added or modified code, please make sure the code compiles before submitting.
- Our automated testing runs [LGTM](https://lgtm.com/projects/g/TheAlgorithms/Go/) on all the pull requests, so please be sure that your code passes before submitting.
- Please conform to [GoDoc](https://blog.golang.org/godoc) standard and document the code as much as possible. This not only facilitates the readers but also generates the correct info on the website.
- **Be consistent in the use of these guidelines.**
#### Documentation
- Make sure you put useful comments in your code. Do not comment on obvious things.
- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure. If you want to create a new directory, then please check if a similar category has been recently suggested or created by other pull requests.
- If you have modified/added documentation, please ensure that your language is concise and must not contain grammatical errors.
- Do not update [`README.md`](https://github.com/TheAlgorithms/Go/blob/master/README.md) along with other changes. First, create an issue and then link to that issue in your pull request to suggest specific changes required to [`README.md`](https://github.com/TheAlgorithms/Go/blob/master/README.md).
- The repository follows [GoDoc](https://blog.golang.org/godoc) standards and auto-generates the [repository website](https://thealgorithms.github.io/). Please ensure the code is documented in this structure. A sample implementation is given below.
#### Test
- Make sure to add examples and test cases in your `filename_test.go` file.
- If you find an algorithm or document without tests, please feel free to create a pull request or issue describing suggested changes.
- Please try to add one or more `Test` functions that will invoke the algorithm implementation on random test data with the expected output.
### Benchmark
---
- Make sure to add examples and benchmark cases in your `filename_test.go` or `filename_bench.go` if you want separated test and benchmark files.
- If you find an algorithm or document without benchmarks, please feel free to create a pull request or issue describing suggested changes.
- Please try to add one or more `Benchmark` functions that will invoke the algorithm implementation.
- For running the benchmark, you could use this command `go test -bench=.` for more details, read this article [Using Subtests and Sub-benchmarks](https://go.dev/blog/subtests)
#### Typical structure of a program
```go
// filename
// description: Add one line description here
// details:
// This is a multi-line
// description containing links, references,
// math equations, etc.
// author(s) [Name](https://github.com/handle), [Name](https://github.com/handle)
// see relatedfile.go, anotherfile.go, file_test.go
// ** Is just an example of how to write description for package and function and other stuff ... **
// Package sort provides primitives for sorting slices and user-defined
// collections.
package sort
// Reasons why you used those packages
// name package : Add one line description here
// name2 package : Add one line description here
// ...
import (
...
)
// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written, and any write error encountered.
func Fprint(w io.Writer, a ...any) (n int, err error) {
...
}
```
#### New File Name guidelines
- Use lowercase words without ``"_"`` for the file name
- Use ``"_"`` as a separator only for `_test.go` or `_bench.go`
- For instance
```markdown
MyNewGoFile.GO is incorrect
my_new_go_file.go is incorrect
mynewgofile.go is the correct format
mynewgofile_test.go is the correct format
```
- It will be used to dynamically create a directory of files and implementation.
- File name validation will run on Docker to ensure validity.
- If an implementation of the algorithm already exists and your version is different from that implemented, please use incremental numeric digit as a suffix. For example: if `binarysearch.go` already exists in the `search` folder, and you are contributing a new implementation, the filename should be `binarysearch2.go` and for a third implementation, `binarysearch3.go`.
- Check out `Go` [Package names](https://go.dev/blog/package-names) roles
#### New Directory guidelines
- We recommend adding files to existing directories as much as possible.
- Use lowercase words with ``"_"`` as separator ( no spaces or ```"-"``` allowed )
- For instance
```markdown
SomeNew Fancy-Category is incorrect
some_new_fancy_category is correct
```
- Filepaths will be used to dynamically create a directory of our algorithms.
- Filepath validation will run on GitHub Actions to ensure compliance.
#### Commit Guidelines
- It is recommended to keep your changes grouped logically within individual commits. Maintainers find it easier to understand changes that are logically spilled across multiple commits. Try to modify just one or two files in the same directory. Pull requests that span multiple directories are often rejected.
```bash
git add filexyz.go
git commit -m "your message"
```
Examples of commit messages with semantic prefixes:
```markdown
fix: XYZ algorithm bug
feat: add XYZ algorithm
test: add test for XYZ algorithm
docs: add comments and explanation to XYZ algorithm
```
Common prefixes:
- fix: A bug fix
- feat: A new feature
- docs: Documentation changes
- test: Correct existing tests or add new ones
### For New Gophers
---
#### Installing Go
- Installation (only needs to be installed once.)
- Mac (using home-brew): `brew install go`
- Windows (MSYS2 64-bit): `choco install golang` [Chocolatey Package Manager](https://chocolatey.org/)
- Linux (Debian): `sudo apt-get install golang`
- Manual Installation: [Downloads - The Go Programming Language](https://golang.org/dl/)
- Running (all platforms): `go run filexyz.go`
> Note: New packages should not be `main`, and never implement `main` functions for any package.
#### Code formatter
To format your code, you can use the gofmt tool directly:
```bash
gofmt -w filexyz.go
```
Or you can use the "go fmt" command:
```bash
go fmt path/to/your/package
```
#### Building
To build you code locally, simply run:
```bash
go build .
```
### Pull Requests
---
- Check out our [pull request template](https://github.com/TheAlgorithms/Go/blob/master/.github/PULL_REQUEST_TEMPLATE/pull_request.md
)
#### Building Locally
Before submitting a pull request, [build the code locally](#building) or using the convenient [](https://gitpod.io/#https://github.com/TheAlgorithms/Go) service.
#### GitHub Actions
- Enable GitHub Actions on your fork of the repository.
After enabling, it will execute `golang_lint_and_test` after every push (not a commit).
- The result can create another commit if the actions made any changes on your behalf.
- Hence, it is better to wait and check the results of GitHub Actions after every push.
- Run `git pull` in your local clone if these actions made many changes to avoid merge conflicts.
Most importantly,
- Happy coding!
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2021 The Algorithms and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# The Algorithms - Go
[](https://gitpod.io/#https://github.com/TheAlgorithms/Go)
[](https://github.com/TheAlgorithms/Go/actions/workflows/ci.yml)
[](https://codecov.io/gh/TheAlgorithms/Go)



[](https://the-algorithms.com/discord/)
### Algorithms implemented in Go (for education)
The repository is a collection of open-source implementation of a variety of algorithms implemented in Go and licensed under [MIT License](LICENSE).
Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
## List of Algorithms
# Packages:
ahocorasick
---
##### Functions:
1. [`Advanced`](./strings/ahocorasick/advancedahocorasick.go#L10): Advanced Function performing the Advanced Aho-Corasick algorithm. Finds and prints occurrences of each pattern.
2. [`AhoCorasick`](./strings/ahocorasick/ahocorasick.go#L15): AhoCorasick Function performing the Basic Aho-Corasick algorithm. Finds and prints occurrences of each pattern.
3. [`ArrayUnion`](./strings/ahocorasick/shared.go#L86): ArrayUnion Concats two arrays of int's into one.
4. [`BoolArrayCapUp`](./strings/ahocorasick/shared.go#L78): BoolArrayCapUp Dynamically increases an array size of bool's by 1.
5. [`BuildAc`](./strings/ahocorasick/ahocorasick.go#L54): Functions that builds Aho Corasick automaton.
6. [`BuildExtendedAc`](./strings/ahocorasick/advancedahocorasick.go#L46): BuildExtendedAc Functions that builds extended Aho Corasick automaton.
7. [`ComputeAlphabet`](./strings/ahocorasick/shared.go#L61): ComputeAlphabet Function that returns string of all the possible characters in given patterns.
8. [`ConstructTrie`](./strings/ahocorasick/shared.go#L4): ConstructTrie Function that constructs Trie as an automaton for a set of reversed & trimmed strings.
9. [`Contains`](./strings/ahocorasick/shared.go#L39): Contains Returns 'true' if array of int's 's' contains int 'e', 'false' otherwise.
10. [`CreateNewState`](./strings/ahocorasick/shared.go#L111): CreateNewState Automaton function for creating a new state 'state'.
11. [`CreateTransition`](./strings/ahocorasick/shared.go#L116): CreateTransition Creates a transition for function σ(state,letter) = end.
12. [`GetParent`](./strings/ahocorasick/shared.go#L99): GetParent Function that finds the first previous state of a state and returns it. Used for trie where there is only one parent.
13. [`GetTransition`](./strings/ahocorasick/shared.go#L121): GetTransition Returns ending state for transition σ(fromState,overChar), '-1' if there is none.
14. [`GetWord`](./strings/ahocorasick/shared.go#L49): GetWord Function that returns word found in text 't' at position range 'begin' to 'end'.
15. [`IntArrayCapUp`](./strings/ahocorasick/shared.go#L70): IntArrayCapUp Dynamically increases an array size of int's by 1.
16. [`StateExists`](./strings/ahocorasick/shared.go#L133): StateExists Checks if state 'state' exists. Returns 'true' if it does, 'false' otherwise.
---
##### Types
1. [`Result`](./strings/ahocorasick/ahocorasick.go#L9): No description provided.
---
armstrong
---
##### Functions:
1. [`IsArmstrong`](./math/armstrong/isarmstrong.go#L14): No description provided.
---
binary
---
##### Package binary describes algorithms that use binary operations for different calculations.
---
##### Functions:
1. [`Abs`](./math/binary/abs.go#L10): Abs returns absolute value using binary operation Principle of operation: 1) Get the mask by right shift by the base 2) Base is the size of an integer variable in bits, for example, for int32 it will be 32, for int64 it will be 64 3) For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers. 4) Add the mask to the given number. 5) XOR of mask + n and mask gives the absolute value.
2. [`BitCounter`](./math/binary/bitcounter.go#L11): BitCounter - The function returns the number of set bits for an unsigned integer number
3. [`FastInverseSqrt`](./math/binary/fast_inverse_sqrt.go#L15): FastInverseSqrt assumes that argument is always positive, and it does not deal with negative numbers. The "magic" number 0x5f3759df is hex for 1597463007 in decimals. The math.Float32bits is alias to *(*uint32)(unsafe.Pointer(&f)) and math.Float32frombits is to *(*float32)(unsafe.Pointer(&b)).
4. [`IsPowerOfTwo`](./math/binary/checkisnumberpoweroftwo.go#L21): IsPowerOfTwo This function uses the fact that powers of 2 are represented like 10...0 in binary, and numbers one less than the power of 2 are represented like 11...1. Therefore, using the and function: 10...0 & 01...1 00...0 -> 0 This is also true for 0, which is not a power of 2, for which we have to add and extra condition.
5. [`IsPowerOfTwoLeftShift`](./math/binary/checkisnumberpoweroftwo.go#L28): IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000, which in decimal system is 8 or = 2 * 2 * 2
6. [`LogBase2`](./math/binary/logarithm.go#L7): LogBase2 Finding the exponent of n = 2**x using bitwise operations (logarithm in base 2 of n) [See more](https://en.wikipedia.org/wiki/Logarithm)
7. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L12): MeanUsingAndXor This function finds arithmetic mean using "AND" and "XOR" operations
8. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L17): MeanUsingRightShift This function finds arithmetic mean using right shift
9. [`ReverseBits`](./math/binary/reversebits.go#L14): ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result and finally move on to the next bit in the input number. Repeat this till all its bits are processed.
10. [`SequenceGrayCode`](./math/binary/rbc.go#L11): SequenceGrayCode The function generates an "Gray code" sequence of length n
11. [`Sqrt`](./math/binary/sqrt.go#L10): No description provided.
12. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L11): XorSearchMissingNumber This function finds a missing number in a sequence
---
cache
---
##### Functions:
1. [`NewLRU`](./cache/lru.go#L22): NewLRU represent initiate lru cache with capacity
2. [`NewLFU`](./cache/lfu.go#L33): NewLFU represent initiate lfu cache with capacity
3. [`Get`](./cache/lfu.go#L52): Get the value by key from LFU cache
4. [`Put`](./cache/lfu.go#L66): Put the key and value in LFU cache
---
##### Types
1. [`LRU`](./cache/lru.go#L15): Default the struct of lru cache algorithm.
2. [`LFU`](./cache/lfu.go#L19): Default the struct of lfu cache algorithm.
---
caesar
---
##### Package caesar is the shift cipher ref: https://en.wikipedia.org/wiki/Caesar_cipher
---
##### Functions:
1. [`Decrypt`](./cipher/caesar/caesar.go#L27): Decrypt decrypts by left shift of "key" each character of "input"
2. [`Encrypt`](./cipher/caesar/caesar.go#L6): Encrypt encrypts by right shift of "key" each character of "input"
3. [`FuzzCaesar`](./cipher/caesar/caesar_test.go#L158): No description provided.
---
catalan
---
##### Functions:
1. [`CatalanNumber`](./math/catalan/catalannumber.go#L16): CatalanNumber This function returns the `nth` Catalan number
---
checksum
---
##### Package checksum describes algorithms for finding various checksums
---
##### Functions:
1. [`CRC8`](./checksum/crc8.go#L25): CRC8 calculates CRC8 checksum of the given data.
2. [`Luhn`](./checksum/luhn.go#L11): Luhn validates the provided data using the Luhn algorithm.
---
##### Types
1. [`CRCModel`](./checksum/crc8.go#L15): No description provided.
---
coloring
---
##### Package coloring provides implementation of different graph coloring algorithms, e.g. coloring using BFS, using Backtracking, using greedy approach. Author(s): [Shivam](https://github.com/Shivam010)
---
##### Functions:
1. [`BipartiteCheck`](./graph/coloring/bipartite.go#L40): basically tries to color the graph in two colors if each edge connects 2 differently colored nodes the graph can be considered bipartite
---
##### Types
1. [`Graph`](./graph/coloring/graph.go#L14): No description provided.
---
combination
---
##### Package combination ...
---
##### Functions:
1. [`Start`](./strings/combination/combination.go#L13): Start ...
---
##### Types
1. [`Combinations`](./strings/combination/combination.go#L7): No description provided.
---
compression
---
##### Functions:
1. [`HuffDecode`](./compression/huffmancoding.go#L104): HuffDecode recursively decodes the binary code in, by traversing the Huffman compression tree pointed by root. current stores the current node of the traversing algorithm. out stores the current decoded string.
2. [`HuffEncode`](./compression/huffmancoding.go#L93): HuffEncode encodes the string in by applying the mapping defined by codes.
3. [`HuffEncoding`](./compression/huffmancoding.go#L76): HuffEncoding recursively traverses the Huffman tree pointed by node to obtain the map codes, that associates a rune with a slice of booleans. Each code is prefixed by prefix and left and right children are labelled with the booleans false and true, respectively.
4. [`HuffTree`](./compression/huffmancoding.go#L33): HuffTree returns the root Node of the Huffman tree by compressing listfreq. The compression produces the most optimal code lengths, provided listfreq is ordered, i.e.: listfreq[i] <= listfreq[j], whenever i < j.
---
##### Types
1. [`Node`](./compression/huffmancoding.go#L17): No description provided.
2. [`SymbolFreq`](./compression/huffmancoding.go#L25): No description provided.
---
compression_test
---
##### Functions:
1. [`SymbolCountOrd`](./compression/huffmancoding_test.go#L16): SymbolCountOrd computes sorted symbol-frequency list of input message
---
conversion
---
##### Package conversion is a package of implementations which converts one data structure to another.
---
##### Functions:
1. [`Base64Decode`](./conversion/base64.go#L57): Base64Decode decodes the received input base64 string into a byte slice. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4
2. [`Base64Encode`](./conversion/base64.go#L19): Base64Encode encodes the received input bytes slice into a base64 string. The implementation follows the RFC4648 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc4648#section-4
3. [`BinaryToDecimal`](./conversion/binarytodecimal.go#L25): BinaryToDecimal() function that will take Binary number as string, and return its Decimal equivalent as integer.
4. [`DecimalToBinary`](./conversion/decimaltobinary.go#L32): DecimalToBinary() function that will take Decimal number as int, and return its Binary equivalent as string.
5. [`FuzzBase64Encode`](./conversion/base64_test.go#L113): No description provided.
6. [`HEXToRGB`](./conversion/rgbhex.go#L10): HEXToRGB splits an RGB input (e.g. a color in hex format; 0x) into the individual components: red, green and blue
7. [`IntToRoman`](./conversion/inttoroman.go#L17): IntToRoman converts an integer value to a roman numeral string. An error is returned if the integer is not between 1 and 3999.
8. [`RGBToHEX`](./conversion/rgbhex.go#L41): RGBToHEX does exactly the opposite of HEXToRGB: it combines the three components red, green and blue to an RGB value, which can be converted to e.g. Hex
9. [`Reverse`](./conversion/decimaltobinary.go#L22): Reverse() function that will take string, and returns the reverse of that string.
10. [`RomanToInt`](./conversion/romantoint.go#L40): RomanToInt converts a roman numeral string to an integer. Roman numerals for numbers outside the range 1 to 3,999 will return an error. Nil or empty string return 0 with no error thrown.
---
deque
---
##### Package deque implements a Double Ended Queue data structure.
---
##### Functions:
1. [`New`](./structure/deque/deque.go#L22): New returns a new DoublyEndedQueue.
---
##### Types
1. [`DoublyEndedQueue`](./structure/deque/deque.go#L17): No description provided.
---
deque_test
---
##### Types
1. [`QueryStructure`](./structure/deque/deque_test.go#L20): No description provided.
2. [`TestCaseData`](./structure/deque/deque_test.go#L27): No description provided.
---
diffiehellman
---
##### Package diffiehellman implements Diffie-Hellman Key Exchange Algorithm for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI
---
##### Functions:
1. [`GenerateMutualKey`](./cipher/diffiehellman/diffiehellmankeyexchange.go#L19): GenerateMutualKey : generates a mutual key that can be used by only alice and bob mutualKey = (shareKey^prvKey)%primeNumber
2. [`GenerateShareKey`](./cipher/diffiehellman/diffiehellmankeyexchange.go#L13): GenerateShareKey : generates a key using client private key , generator and primeNumber this key can be made public shareKey = (g^key)%primeNumber
---
dynamic
---
##### Package dynamic is a package of certain implementations of dynamically run algorithms.
---
##### Functions:
1. [`Abbreviation`](./dynamic/abbreviation.go#L24): Returns true if it is possible to make a equals b (if b is an abbreviation of a), returns false otherwise
2. [`Bin2`](./dynamic/binomialcoefficient.go#L21): Bin2 function
3. [`CoinChange`](./dynamic/coinchange.go#L5): CoinChange finds the number of possible combinations of coins of different values which can get to the target amount.
4. [`CutRodDp`](./dynamic/rodcutting.go#L21): CutRodDp solve the same problem using dynamic programming
5. [`CutRodRec`](./dynamic/rodcutting.go#L8): CutRodRec solve the problem recursively: initial approach
6. [`EditDistanceDP`](./dynamic/editdistance.go#L35): EditDistanceDP is an optimised implementation which builds on the ideas of the recursive implementation. We use dynamic programming to compute the DP table where dp[i][j] denotes the edit distance value of first[0..i-1] and second[0..j-1]. Time complexity is O(m * n) where m and n are lengths of the strings, first and second respectively.
7. [`EditDistanceRecursive`](./dynamic/editdistance.go#L10): EditDistanceRecursive is a naive implementation with exponential time complexity.
8. [`IsSubsetSum`](./dynamic/subsetsum.go#L14): No description provided.
9. [`Knapsack`](./dynamic/knapsack.go#L17): Knapsack solves knapsack problem return maxProfit
10. [`LongestCommonSubsequence`](./dynamic/longestcommonsubsequence.go#L13): LongestCommonSubsequence function
11. [`LongestIncreasingSubsequence`](./dynamic/longestincreasingsubsequence.go#L9): LongestIncreasingSubsequence returns the longest increasing subsequence where all elements of the subsequence are sorted in increasing order
12. [`LongestIncreasingSubsequenceGreedy`](./dynamic/longestincreasingsubsequencegreedy.go#L9): LongestIncreasingSubsequenceGreedy is a function to find the longest increasing subsequence in a given array using a greedy approach. The dynamic programming approach is implemented alongside this one. Worst Case Time Complexity: O(nlogn) Auxiliary Space: O(n), where n is the length of the array(slice). Reference: https://www.geeksforgeeks.org/construction-of-longest-monotonically-increasing-subsequence-n-log-n/
13. [`LpsDp`](./dynamic/longestpalindromicsubsequence.go#L25): LpsDp function
14. [`LpsRec`](./dynamic/longestpalindromicsubsequence.go#L20): LpsRec function
15. [`MatrixChainDp`](./dynamic/matrixmultiplication.go#L24): MatrixChainDp function
16. [`MatrixChainRec`](./dynamic/matrixmultiplication.go#L10): MatrixChainRec function
17. [`Max`](./dynamic/knapsack.go#L11): Max function - possible duplicate
18. [`NthCatalanNumber`](./dynamic/catalan.go#L13): NthCatalan returns the n-th Catalan Number Complexity: O(n²)
19. [`NthFibonacci`](./dynamic/fibonacci.go#L6): NthFibonacci returns the nth Fibonacci Number
20. [`TrapRainWater`](./dynamic/traprainwater.go#L17): Calculate the amount of trapped rainwater between bars represented by an elevation map using dynamic programming.
---
dynamicarray
---
##### Package dynamicarray A dynamic array is quite similar to a regular array, but its Size is modifiable during program runtime, very similar to how a slice in Go works. The implementation is for educational purposes and explains how one might go about implementing their own version of slices. For more details check out those links below here: GeeksForGeeks article : https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/ Go blog: https://blog.golang.org/slices-intro Go blog: https://blog.golang.org/slices authors [Wesllhey Holanda](https://github.com/wesllhey), [Milad](https://github.com/miraddo) see dynamicarray.go, dynamicarray_test.go
---
##### Types
1. [`DynamicArray`](./structure/dynamicarray/dynamicarray.go#L21): No description provided.
---
factorial
---
##### Package factorial describes algorithms Factorials calculations.
---
##### Functions:
1. [`Iterative`](./math/factorial/factorial.go#L12): Iterative returns the iteratively brute forced factorial of n
2. [`Recursive`](./math/factorial/factorial.go#L21): Recursive This function recursively computes the factorial of a number
3. [`UsingTree`](./math/factorial/factorial.go#L30): UsingTree This function finds the factorial of a number using a binary tree
---
fibonacci
---
##### Functions:
1. [`Formula`](./math/fibonacci/fibonacci.go#L42): Formula This function calculates the n-th fibonacci number using the [formula](https://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio) Attention! Tests for large values fall due to rounding error of floating point numbers, works well, only on small numbers
2. [`Matrix`](./math/fibonacci/fibonacci.go#L15): Matrix This function calculates the n-th fibonacci number using the matrix method. [See](https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form)
3. [`Recursive`](./math/fibonacci/fibonacci.go#L51): Recursive calculates the n-th fibonacci number recursively by adding the previous two Fibonacci numbers. This algorithm is extremely slow for bigger numbers, but provides a simpler implementation.
---
gcd
---
##### Functions:
1. [`Extended`](./math/gcd/extended.go#L12): Extended simple extended gcd
2. [`ExtendedIterative`](./math/gcd/extendedgcditerative.go#L4): ExtendedIterative finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b).
3. [`ExtendedRecursive`](./math/gcd/extendedgcd.go#L4): ExtendedRecursive finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b).
4. [`Iterative`](./math/gcd/gcditerative.go#L4): Iterative Faster iterative version of GcdRecursive without holding up too much of the stack
5. [`Recursive`](./math/gcd/gcd.go#L4): Recursive finds and returns the greatest common divisor of a given integer.
6. [`TemplateBenchmarkExtendedGCD`](./math/gcd/extendedgcd_test.go#L44): No description provided.
7. [`TemplateBenchmarkGCD`](./math/gcd/gcd_test.go#L37): No description provided.
8. [`TemplateTestExtendedGCD`](./math/gcd/extendedgcd_test.go#L7): No description provided.
9. [`TemplateTestGCD`](./math/gcd/gcd_test.go#L18): No description provided.
---
generateparentheses
---
##### Functions:
1. [`GenerateParenthesis`](./strings/generateparentheses/generateparentheses.go#L12): No description provided.
---
genetic
---
##### Package genetic provides functions to work with strings using genetic algorithm. https://en.wikipedia.org/wiki/Genetic_algorithm Author: D4rkia
---
##### Functions:
1. [`GeneticString`](./strings/genetic/genetic.go#L71): GeneticString generates PopulationItem based on the imputed target string, and a set of possible runes to build a string with. In order to optimise string generation additional configurations can be provided with Conf instance. Empty instance of Conf (&Conf{}) can be provided, then default values would be set. Link to the same algorithm implemented in python: https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_string.py
---
##### Types
1. [`Conf`](./strings/genetic/genetic.go#L32): No description provided.
2. [`PopulationItem`](./strings/genetic/genetic.go#L26): No description provided.
3. [`Result`](./strings/genetic/genetic.go#L52): No description provided.
---
geometry
---
##### Package geometry contains geometric algorithms Package geometry contains geometric algorithms
---
##### Functions:
1. [`Distance`](./math/geometry/straightlines.go#L18): Distance calculates the shortest distance between two points.
2. [`EuclideanDistance`](./math/geometry/distance.go#L20): EuclideanDistance returns the Euclidean distance between points in any `n` dimensional Euclidean space.
3. [`IsParallel`](./math/geometry/straightlines.go#L42): IsParallel checks if two lines are parallel or not.
4. [`IsPerpendicular`](./math/geometry/straightlines.go#L47): IsPerpendicular checks if two lines are perpendicular or not.
5. [`PointDistance`](./math/geometry/straightlines.go#L53): PointDistance calculates the distance of a given Point from a given line. The slice should contain the coefficiet of x, the coefficient of y and the constant in the respective order.
6. [`Section`](./math/geometry/straightlines.go#L24): Section calculates the Point that divides a line in specific ratio. DO NOT specify the ratio in the form m:n, specify it as r, where r = m / n.
7. [`Slope`](./math/geometry/straightlines.go#L32): Slope calculates the slope (gradient) of a line.
8. [`YIntercept`](./math/geometry/straightlines.go#L37): YIntercept calculates the Y-Intercept of a line from a specific Point.
---
##### Types
1. [`EuclideanPoint`](./math/geometry/distance.go#L14): No description provided.
2. [`Line`](./math/geometry/straightlines.go#L13): No description provided.
3. [`Point`](./math/geometry/straightlines.go#L9): No description provided.
---
graph
---
##### Package graph demonstrates Graph search algorithms reference: https://en.wikipedia.org/wiki/Tree_traversal
---
##### Functions:
1. [`ArticulationPoint`](./graph/articulationpoints.go#L19): ArticulationPoint is a function to identify articulation points in a graph. The function takes the graph as an argument and returns a boolean slice which indicates whether a vertex is an articulation point or not. Worst Case Time Complexity: O(|V| + |E|) Auxiliary Space: O(|V|) reference: https://en.wikipedia.org/wiki/Biconnected_component and https://cptalks.quora.com/Cut-Vertex-Articulation-point
2. [`BreadthFirstSearch`](./graph/breadthfirstsearch.go#L9): BreadthFirstSearch is an algorithm for traversing and searching graph data structures. It starts at an arbitrary node of a graph, and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. Worst-case performance O(|V|+|E|)=O(b^{d})}O(|V|+|E|)=O(b^{d}) Worst-case space complexity O(|V|)=O(b^{d})}O(|V|)=O(b^{d}) reference: https://en.wikipedia.org/wiki/Breadth-first_search
3. [`DepthFirstSearch`](./graph/depthfirstsearch.go#L53): No description provided.
4. [`DepthFirstSearchHelper`](./graph/depthfirstsearch.go#L21): No description provided.
5. [`FloydWarshall`](./graph/floydwarshall.go#L15): FloydWarshall Returns all pair's shortest path using Floyd Warshall algorithm
6. [`GetIdx`](./graph/depthfirstsearch.go#L3): No description provided.
7. [`KruskalMST`](./graph/kruskal.go#L23): No description provided.
8. [`PrimMST`](./graph/prim.go#30): Computes the minimum spanning tree of a weighted undirected graph
9. [`LowestCommonAncestor`](./graph/lowestcommonancestor.go#L111): For each node, we will precompute its ancestor above him, its ancestor two nodes above, its ancestor four nodes above, etc. Let's call `jump[j][u]` is the `2^j`-th ancestor above the node `u` with `u` in range `[0, numbersVertex)`, `j` in range `[0,MAXLOG)`. These information allow us to jump from any node to any ancestor above it in `O(MAXLOG)` time.
10. [`New`](./graph/graph.go#L16): Constructor functions for graphs (undirected by default)
11. [`NewTree`](./graph/lowestcommonancestor.go#L84): No description provided.
12. [`NewUnionFind`](./graph/unionfind.go#L24): Initialise a new union find data structure with s nodes
13. [`NotExist`](./graph/depthfirstsearch.go#L12): No description provided.
14. [`Topological`](./graph/topological.go#L7): Topological assumes that graph given is valid and that its possible to get a topological ordering. constraints are array of []int{a, b}, representing an edge going from a to b
15. [`Edmond-Karp`](./graph/edmondkarp.go#L43): Computes the maximum possible flow between a pair of s-t vertices in a weighted graph
---
##### Types
1. [`Edge`](./graph/kruskal.go#L17): No description provided.
2. [`Graph`](./graph/graph.go#L9): No description provided.
3. [`Item`](./graph/dijkstra.go#L5): No description provided.
4. [`Query`](./graph/lowestcommonancestor_test.go#L9): No description provided.
5. [`Tree`](./graph/lowestcommonancestor.go#L25): No description provided.
6. [`TreeEdge`](./graph/lowestcommonancestor.go#L12): No description provided.
7. [`UnionFind`](./graph/unionfind.go#L18): No description provided.
8. [`WeightedGraph`](./graph/floydwarshall.go#L9): No description provided.
---
guid
---
##### Package guid provides facilities for generating random globally unique identifiers.
---
##### Functions:
1. [`New`](./strings/guid/guid.go#L28): New returns a randomly generated global unique identifier.
---
hashmap
---
##### Functions:
1. [`Make`](./structure/hashmap/hashmap.go#L32): Make creates a new HashMap instance with input size and capacity
2. [`New`](./structure/hashmap/hashmap.go#L24): New return new HashMap instance
---
##### Types
1. [`HashMap`](./structure/hashmap/hashmap.go#L17): No description provided.
---
heap
---
##### Functions:
1. [`New`](./structure/heap/heap.go#L15): New gives a new heap object.
2. [`NewAny`](./structure/heap/heap.go#L24): NewAny gives a new heap object. element can be anything, but must provide less function.
---
##### Types
1. [`Heap`](./structure/heap/heap.go#L9): No description provided.
---
heap_test
---
##### Types
1. [`testInt`](#L0):
Methods:
1. [`Less`](./structure/heap/heap_test.go#L11): No description provided.
2. [`testStudent`](#L0):
Methods:
1. [`Less`](./structure/heap/heap_test.go#L20): No description provided.
---
horspool
---
##### Functions:
1. [`Horspool`](./strings/horspool/horspool.go#L10): No description provided.
---
kmp
---
##### Functions:
1. [`Kmp`](./strings/kmp/kmp.go#L4): Kmp Function kmp performing the Knuth-Morris-Pratt algorithm.
---
##### Types
1. [`args`](./strings/kmp/kmp_test.go#L39): No description provided.
---
lcm
---
##### Functions:
1. [`Lcm`](./math/lcm/lcm.go#L10): Lcm returns the lcm of two numbers using the fact that lcm(a,b) * gcd(a,b) = | a * b |
---
levenshtein
---
##### Functions:
1. [`Distance`](./strings/levenshtein/levenshteindistance.go#L10): Distance Function that gives Levenshtein Distance
---
linkedlist
---
##### Package linkedlist demonstrates different implementations on linkedlists.
---
##### Functions:
1. [`JosephusProblem`](./structure/linkedlist/cyclic.go#L120): https://en.wikipedia.org/wiki/Josephus_problem This is a struct-based solution for Josephus problem.
2. [`NewCyclic`](./structure/linkedlist/cyclic.go#L12): Create new list.
3. [`NewDoubly`](./structure/linkedlist/doubly.go#L31): No description provided.
4. [`NewNode`](./structure/linkedlist/shared.go#L12): Create new node.
5. [`NewSingly`](./structure/linkedlist/singlylinkedlist.go#L19): NewSingly returns a new instance of a linked list
---
##### Types
1. [`Cyclic`](./structure/linkedlist/cyclic.go#L6): No description provided.
2. [`Doubly`](./structure/linkedlist/doubly.go#L18): No description provided.
3. [`Node`](./structure/linkedlist/shared.go#L5): No description provided.
4. [`Singly`](./structure/linkedlist/singlylinkedlist.go#L10): No description provided.
5. [`testCase`](./structure/linkedlist/cyclic_test.go#L105): No description provided.
---
manacher
---
##### Functions:
1. [`LongestPalindrome`](./strings/manacher/longestpalindrome.go#L37): No description provided.
---
math
---
##### Package math is a package that contains mathematical algorithms and its different implementations. filename : krishnamurthy.go description: A program which contains the function that returns true if a given number is Krishnamurthy number or not. details: A number is a Krishnamurthy number if the sum of all the factorials of the digits is equal to the number. Ex: 1! = 1, 145 = 1! + 4! + 5! author(s): [GooMonk](https://github.com/GooMonk) see krishnamurthy_test.go
---
##### Functions:
1. [`Abs`](./math/abs.go#L11): Abs returns absolute value
2. [`AliquotSum`](./math/aliquotsum.go#L14): This function returns s(n) for given number
3. [`Combinations`](./math/binomialcoefficient.go#L20): C is Binomial Coefficient function This function returns C(n, k) for given n and k
4. [`Cos`](./math/cos.go#L10): Cos returns the cosine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine) [Based on the idea of Bhaskara approximation of cos(x)](https://math.stackexchange.com/questions/3886552/bhaskara-approximation-of-cosx)
5. [`DefaultPolynomial`](./math/pollard.go#L16): DefaultPolynomial is the commonly used polynomial g(x) = (x^2 + 1) mod n
6. [`FindKthMax`](./math/kthnumber.go#L11): FindKthMax returns the kth large element given an integer slice with nil `error` if found and returns -1 with `error` `search.ErrNotFound` if not found. NOTE: The `nums` slice gets mutated in the process.
7. [`FindKthMin`](./math/kthnumber.go#L19): FindKthMin returns kth small element given an integer slice with nil `error` if found and returns -1 with `error` `search.ErrNotFound` if not found. NOTE: The `nums` slice gets mutated in the process.
8. [`IsAutomorphic`](./math/isautomorphic.go#L16): No description provided.
9. [`IsKrishnamurthyNumber`](./math/krishnamurthy.go#L12): IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not.
10. [`IsPerfectNumber`](./math/perfectnumber.go#L34): Checks if inNumber is a perfect number
11. [`IsPowOfTwoUseLog`](./math/checkisnumberpoweroftwo.go#L10): IsPowOfTwoUseLog This function checks if a number is a power of two using the logarithm. The limiting degree can be from 0 to 63. See alternatives in the binary package.
12. [`Lerp`](./math/lerp.go#L5): Lerp or Linear interpolation This function will return new value in 't' percentage between 'v0' and 'v1'
13. [`LiouvilleLambda`](./math/liouville.go#L24): Lambda is the liouville function This function returns λ(n) for given number
14. [`Mean`](./math/mean.go#L7): No description provided.
15. [`Median`](./math/median.go#L12): No description provided.
16. [`Mode`](./math/mode.go#L19): No description provided.
17. [`Mu`](./math/mobius.go#L21): Mu is the Mobius function This function returns μ(n) for given number
18. [`Phi`](./math/eulertotient.go#L5): Phi is the Euler totient function. This function computes the number of numbers less then n that are coprime with n.
19. [`PollardsRhoFactorization`](./math/pollard.go#L29): PollardsRhoFactorization is an implementation of Pollard's rho factorization algorithm using the default parameters x = y = 2
20. [`PronicNumber`](./math/pronicnumber.go#L15): PronicNumber returns true if argument passed to the function is pronic and false otherwise.
21. [`Sin`](./math/sin.go#L9): Sin returns the sine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine)
22. [`SumOfProperDivisors`](./math/perfectnumber.go#L17): Returns the sum of proper divisors of inNumber.
---
matrix
---
##### filename: strassenmatrixmultiply.go description: Implements matrix multiplication using the Strassen algorithm. details: This program takes two matrices as input and performs matrix multiplication using the Strassen algorithm, which is an optimized divide-and-conquer approach. It allows for efficient multiplication of large matrices. author(s): Mohit Raghav(https://github.com/mohit07raghav19) See strassenmatrixmultiply_test.go for test cases
---
##### Functions:
1. [`IsValid`](./math/matrix/isvalid.go#L6): IsValid checks if the input matrix has consistent row lengths.
2. [`New`](./math/matrix/matrix.go#L17): NewMatrix creates a new Matrix based on the provided arguments.
3. [`NewFromElements`](./math/matrix/matrix.go#L43): NewFromElements creates a new Matrix from the given elements.
---
##### Types
1. [`Matrix`](./math/matrix/matrix.go#L10): No description provided.
---
matrix_test
---
##### Functions:
1. [`MakeRandomMatrix`](./math/matrix/strassenmatrixmultiply_test.go#L105): No description provided.
---
max
---
##### Functions:
1. [`Bitwise`](./math/max/bitwisemax.go#L11): Bitwise computes using bitwise operator the maximum of all the integer input and returns it
2. [`Int`](./math/max/max.go#L6): Int is a function which returns the maximum of all the integers provided as arguments.
---
maxsubarraysum
---
##### Package maxsubarraysum is a package containing a solution to a common problem of finding max contiguous sum within a array of ints.
---
##### Functions:
1. [`MaxSubarraySum`](./other/maxsubarraysum/maxsubarraysum.go#L13): MaxSubarraySum returns the maximum subarray sum
---
min
---
##### Functions:
1. [`Bitwise`](./math/min/bitwisemin.go#L11): Bitwise This function returns the minimum integer using bit operations
2. [`Int`](./math/min/min.go#L6): Int is a function which returns the minimum of all the integers provided as arguments.
---
modular
---
##### Functions:
1. [`Exponentiation`](./math/modular/exponentiation.go#L22): Exponentiation returns base^exponent % mod
2. [`Inverse`](./math/modular/inverse.go#L19): Inverse Modular function
3. [`Multiply64BitInt`](./math/modular/exponentiation.go#L51): Multiply64BitInt Checking if the integer multiplication overflows
---
moserdebruijnsequence
---
##### Functions:
1. [`MoserDeBruijnSequence`](./math/moserdebruijnsequence/sequence.go#L7): No description provided.
---
nested
---
##### Package nested provides functions for testing strings proper brackets nesting.
---
##### Functions:
1. [`IsBalanced`](./other/nested/nestedbrackets.go#L20): IsBalanced returns true if provided input string is properly nested. Input is a sequence of brackets: '(', ')', '[', ']', '{', '}'. A sequence of brackets `s` is considered properly nested if any of the following conditions are true: - `s` is empty; - `s` has the form (U) or [U] or {U} where U is a properly nested string; - `s` has the form VW where V and W are properly nested strings. For example, the string "()()[()]" is properly nested but "[(()]" is not. **Note** Providing characters other then brackets would return false, despite brackets sequence in the string. Make sure to filter input before usage.
---
palindrome
---
##### Functions:
1. [`IsPalindrome`](./strings/palindrome/ispalindrome.go#L26): No description provided.
2. [`IsPalindromeRecursive`](./strings/palindrome/ispalindrome.go#L39): No description provided.
---
pangram
---
##### Functions:
1. [`IsPangram`](./strings/pangram/ispangram.go#L21): No description provided.
---
parenthesis
---
##### Functions:
1. [`Parenthesis`](./strings/parenthesis/parenthesis.go#L8): Parenthesis algorithm checks if every opened parenthesis is closed correctly. When parcounter is less than 0 when a closing parenthesis is detected without an opening parenthesis that surrounds it and parcounter will be 0 if all open parenthesis are closed correctly.
---
pascal
---
##### Functions:
1. [`GenerateTriangle`](./math/pascal/pascaltriangle.go#L24): GenerateTriangle This function generates a Pascal's triangle of n lines
---
password
---
##### Package password contains functions to help generate random passwords
---
##### Functions:
1. [`Generate`](./other/password/generator.go#L15): Generate returns a newly generated password
---
permutation
---
##### Functions:
1. [`GenerateElementSet`](./math/permutation/heaps.go#L37): No description provided.
2. [`Heaps`](./math/permutation/heaps.go#L8): Heap's Algorithm for generating all permutations of n objects
3. [`NextPermutation`](./math/permutation/next_permutation.go#8): A solution to find next permutation of an integer array in constant memory
---
pi
---
##### spigotpi_test.go description: Test for Spigot Algorithm for the Digits of Pi author(s) [red_byte](https://github.com/i-redbyte) see spigotpi.go
---
##### Functions:
1. [`MonteCarloPi`](./math/pi/montecarlopi.go#L17): No description provided.
2. [`MonteCarloPiConcurrent`](./math/pi/montecarlopi.go#L36): MonteCarloPiConcurrent approximates the value of pi using the Monte Carlo method. Unlike the MonteCarloPi function (first version), this implementation uses goroutines and channels to parallelize the computation. More details on the Monte Carlo method available at https://en.wikipedia.org/wiki/Monte_Carlo_method. More details on goroutines parallelization available at https://go.dev/doc/effective_go#parallel.
3. [`Spigot`](./math/pi/spigotpi.go#L12): No description provided.
---
polybius
---
##### Package polybius is encrypting method with polybius square ref: https://en.wikipedia.org/wiki/Polybius_square#Hybrid_Polybius_Playfair_Cipher
---
##### Functions:
1. [`FuzzPolybius`](./cipher/polybius/polybius_test.go#L154): No description provided.
2. [`NewPolybius`](./cipher/polybius/polybius.go#L21): NewPolybius returns a pointer to object of Polybius. If the size of "chars" is longer than "size", "chars" are truncated to "size".
---
##### Types
1. [`Polybius`](./cipher/polybius/polybius.go#L12): No description provided.
---
power
---
##### Functions:
1. [`IterativePower`](./math/power/fastexponent.go#L4): IterativePower is iterative O(logn) function for pow(x, y)
2. [`RecursivePower`](./math/power/fastexponent.go#L18): RecursivePower is recursive O(logn) function for pow(x, y)
3. [`RecursivePower1`](./math/power/fastexponent.go#L30): RecursivePower1 is recursive O(n) function for pow(x, y)
4. [`UsingLog`](./math/power/powvialogarithm.go#L14): No description provided.
---
prime
---
##### Functions:
1. [`Factorize`](./math/prime/primefactorization.go#L5): Factorize is a function that computes the exponents of each prime in the prime factorization of n
2. [`Generate`](./math/prime/sieve.go#L26): Generate returns a int slice of prime numbers up to the limit
3. [`GenerateChannel`](./math/prime/sieve.go#L9): Generate generates the sequence of integers starting at 2 and sends it to the channel `ch`
4. [`MillerRabinDeterministic`](./math/prime/millerrabintest.go#L121): MillerRabinDeterministic is a Deterministic version of the Miller-Rabin test, which returns correct results for all valid int64 numbers.
5. [`MillerRabinProbabilistic`](./math/prime/millerrabintest.go#L101): MillerRabinProbabilistic is a probabilistic test for primality of an integer based of the algorithm devised by Miller and Rabin.
6. [`MillerRandomTest`](./math/prime/millerrabintest.go#L77): MillerRandomTest This is the intermediate step that repeats within the miller rabin primality test for better probabilitic chances of receiving the correct result with random witnesses.
7. [`MillerTest`](./math/prime/millerrabintest.go#L49): MillerTest tests whether num is a strong probable prime to a witness. Formally: a^d ≡ 1 (mod n) or a^(2^r * d) ≡ -1 (mod n), 0 <= r <= s
8. [`MillerTestMultiple`](./math/prime/millerrabintest.go#L84): MillerTestMultiple is like MillerTest but runs the test for multiple witnesses.
9. [`OptimizedTrialDivision`](./math/prime/primecheck.go#L26): OptimizedTrialDivision checks primality of an integer using an optimized trial division method. The optimizations include not checking divisibility by the even numbers and only checking up to the square root of the given number.
10. [`Sieve`](./math/prime/sieve.go#L16): Sieve Sieving the numbers that are not prime from the channel - basically removing them from the channels
11. [`TrialDivision`](./math/prime/primecheck.go#L9): TrialDivision tests whether a number is prime by trying to divide it by the numbers less than it.
12. [`Twin`](./math/prime/twin.go#L15): This function returns twin prime for given number returns (n + 2) if both n and (n + 2) are prime -1 otherwise
---
pythagoras
---
##### Functions:
1. [`Distance`](./math/pythagoras/pythagoras.go#L15): Distance calculates the distance between to vectors with the Pythagoras theorem
---
##### Types
1. [`Vector`](./math/pythagoras/pythagoras.go#L8): No description provided.
---
queue
---
##### Functions:
1. [`BackQueue`](./structure/queue/queuearray.go#L32): BackQueue return the Back value
2. [`DeQueue`](./structure/queue/queuearray.go#L20): DeQueue it will be removed the first value that added into the list
3. [`EnQueue`](./structure/queue/queuearray.go#L15): EnQueue it will be added new value into our list
4. [`FrontQueue`](./structure/queue/queuearray.go#L27): FrontQueue return the Front value
5. [`IsEmptyQueue`](./structure/queue/queuearray.go#L42): IsEmptyQueue check our list is empty or not
6. [`LenQueue`](./structure/queue/queuearray.go#L37): LenQueue will return the length of the queue list
---
##### Types
1. [`LQueue`](./structure/queue/queuelinklistwithlist.go#L20): No description provided.
2. [`Node`](./structure/queue/queuelinkedlist.go#L13): No description provided.
3. [`Queue`](./structure/queue/queuelinkedlist.go#L19): No description provided.
---
rot13
---
##### Package rot13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. ref: https://en.wikipedia.org/wiki/ROT13
---
##### Functions:
1. [`FuzzRot13`](./cipher/rot13/rot13_test.go#L72): No description provided.
---
rsa
---
##### Package rsa shows a simple implementation of RSA algorithm
---
##### Functions:
1. [`Decrypt`](./cipher/rsa/rsa.go#L43): Decrypt decrypts encrypted rune slice based on the RSA algorithm
2. [`Encrypt`](./cipher/rsa/rsa.go#L28): Encrypt encrypts based on the RSA algorithm - uses modular exponentitation in math directory
3. [`FuzzRsa`](./cipher/rsa/rsa_test.go#L79): No description provided.
---
search
---
##### Functions:
1. [`BoyerMoore`](./strings/search/boyermoore.go#L5): Implementation of boyer moore string search O(l) where l=len(text)
2. [`Naive`](./strings/search/naive.go#L5): Implementation of naive string search O(n*m) where n=len(txt) and m=len(pattern)
---
segmenttree
---
##### Functions:
1. [`NewSegmentTree`](./structure/segmenttree/segmenttree.go#L116): No description provided.
---
##### Types
1. [`SegmentTree`](./structure/segmenttree/segmenttree.go#L17): No description provided.
---
set
---
##### package set implements a Set using a golang map. This implies that only the types that are accepted as valid map keys can be used as set elements. For instance, do not try to Add a slice, or the program will panic.
---
##### Functions:
1. [`New`](./structure/set/set.go#L7): New gives new set.
---
sha256
---
##### Functions:
1. [`Hash`](./hashing/sha256/sha256.go#L50): Hash hashes the input message using the sha256 hashing function, and return a 32 byte array. The implementation follows the RGC6234 standard, which is documented at https://datatracker.ietf.org/doc/html/rfc6234
---
sort
---
##### Package sort a package for demonstrating sorting algorithms in Go
---
##### Functions:
1. [`BinaryInsertion`](./sort/binaryinsertionsort.go#L13): No description provided.
2. [`Bogo`](./sort/bogosort.go#L32): No description provided.
3. [`Bubble`](./sort/bubblesort.go#L9): Bubble is a simple generic definition of Bubble sort algorithm.
4. [`Bucket`](./sort/bucketsort.go#L7): Bucket sorts a slice. It is mainly useful when input is uniformly distributed over a range.
5. [`Cocktail`](./sort/cocktailsort.go#L9): Cocktail sort is a variation of bubble sort, operating in two directions (beginning to end, end to beginning)
6. [`Comb`](./sort/combSort.go#L17): Comb is a simple sorting algorithm which is an improvement of the bubble sorting algorithm.
7. [`Count`](./sort/countingsort.go#L11): No description provided.
8. [`Cycle`](./sort/cyclesort.go#L10): Cycle sort is an in-place, unstable sorting algorithm that is particularly useful when sorting arrays containing elements with a small range of values. It is theoretically optimal in terms of the total number of writes to the original array.
9. [`Exchange`](./sort/exchangesort.go#L8): No description provided.
10. [`HeapSort`](./sort/heapsort.go#L116): No description provided.
11. [`ImprovedSimple`](./sort/simplesort.go#L27): ImprovedSimple is a improve SimpleSort by skipping an unnecessary comparison of the first and last. This improved version is more similar to implementation of insertion sort
12. [`Insertion`](./sort/insertionsort.go#L5): No description provided.
13. [`Merge`](./sort/mergesort.go#L41): Merge Perform merge sort on a slice
14. [`MergeIter`](./sort/mergesort.go#L55): No description provided.
15. [`Pancake`](./sort/pancakesort.go#L8): Pancake sorts a slice using flip operations, where flip refers to the idea of reversing the slice from index `0` to `i`.
16. [`ParallelMerge`](./sort/mergesort.go#L66): ParallelMerge Perform merge sort on a slice using goroutines
17. [`Partition`](./sort/quicksort.go#L12): No description provided.
18. [`Patience`](./sort/patiencesort.go#L13): No description provided.
19. [`Pigeonhole`](./sort/pigeonholesort.go#L15): Pigeonhole sorts a slice using pigeonhole sorting algorithm. NOTE: To maintain time complexity O(n + N), this is the reason for having only Integer constraint instead of Ordered.
20. [`Quicksort`](./sort/quicksort.go#L39): Quicksort Sorts the entire array
21. [`QuicksortRange`](./sort/quicksort.go#L26): QuicksortRange Sorts the specified range within the array
22. [`RadixSort`](./sort/radixsort.go#L43): No description provided.
23. [`Selection`](./sort/selectionsort.go#L5): No description provided.
24. [`Shell`](./sort/shellsort.go#L5): No description provided.
25. [`Simple`](./sort/simplesort.go#L13): No description provided.
---
##### Types
1. [`MaxHeap`](./sort/heapsort.go#L5): No description provided.
---
sqrt
---
##### Package sqrt contains algorithms and data structures that contains a √n in their complexity
---
##### Functions:
1. [`NewSqrtDecomposition`](./sqrt/sqrtdecomposition.go#L34): Create a new SqrtDecomposition instance with the parameters as specified by SqrtDecomposition comment Assumptions: - len(elements) > 0
---
##### Types
1. [`SqrtDecomposition`](./sqrt/sqrtdecomposition.go#L21): No description provided.
---
stack
---
##### Functions:
1. [`NewStack`](./structure/stack/stackarray.go#L17): NewStack creates and returns a new stack.
---
##### Types
1. [`Array`](./structure/stack/stackarray.go#L12): No description provided.
2. [`Node`](./structure/stack/stacklinkedlist.go#L13): No description provided.
3. [`SList`](./structure/stack/stacklinkedlistwithlist.go#L18): No description provided.
4. [`Stack`](./structure/stack/stacklinkedlist.go#L19): No description provided.
---
strings
---
##### Package strings is a package that contains all algorithms that are used to analyse and manipulate strings.
---
##### Functions:
1. [`CountChars`](./strings/charoccurrence.go#L12): CountChars counts the number of a times a character has occurred in the provided string argument and returns a map with `rune` as keys and the count as value.
2. [`IsIsogram`](./strings/isisogram.go#L34): No description provided.
3. [`IsSubsequence`](./strings/issubsequence.go#L10): Returns true if s is subsequence of t, otherwise return false.
---
transposition
---
##### Functions:
1. [`Decrypt`](./cipher/transposition/transposition.go#L81): No description provided.
2. [`Encrypt`](./cipher/transposition/transposition.go#L51): No description provided.
3. [`FuzzTransposition`](./cipher/transposition/transposition_test.go#L103): No description provided.
---
tree
---
##### For more details check out those links below here: Wikipedia article: https://en.wikipedia.org/wiki/Binary_search_tree authors [guuzaa](https://github.com/guuzaa)
---
##### Functions:
1. [`NewAVL`](./structure/tree/avl.go#L54): NewAVL creates a novel AVL tree
2. [`NewBinarySearch`](./structure/tree/bstree.go#L46): NewBinarySearch creates a novel Binary-Search tree
3. [`NewRB`](./structure/tree/rbtree.go#L57): NewRB creates a new Red-Black Tree
---
##### Types
1. [`AVL`](./structure/tree/avl.go#L48): No description provided.
2. [`AVLNode`](./structure/tree/avl.go#L18): No description provided.
3. [`BSNode`](./structure/tree/bstree.go#L15): No description provided.
4. [`BinarySearch`](./structure/tree/bstree.go#L40): No description provided.
5. [`RB`](./structure/tree/rbtree.go#L51): No description provided.
6. [`RBNode`](./structure/tree/rbtree.go#L25): No description provided.
---
trie
---
##### Package trie provides Trie data structures in golang. Wikipedia: https://en.wikipedia.org/wiki/Trie
---
##### Functions:
1. [`NewNode`](./structure/trie/trie.go#L14): NewNode creates a new Trie node with initialized children map.
---
##### Types
1. [`Node`](./structure/trie/trie.go#L7): No description provided.
---
xor
---
##### Package xor is an encryption algorithm that operates the exclusive disjunction(XOR) ref: https://en.wikipedia.org/wiki/XOR_cipher
---
##### Functions:
1. [`Decrypt`](./cipher/xor/xor.go#L19): Decrypt decrypts with Xor encryption
2. [`Encrypt`](./cipher/xor/xor.go#L10): Encrypt encrypts with Xor encryption after converting each character to byte The returned value might not be readable because there is no guarantee which is within the ASCII range If using other type such as string, []int, or some other types, add the statements for converting the type to []byte.
3. [`FuzzXOR`](./cipher/xor/xor_test.go#L108): No description provided.
---
##### Package rail fence is a classical type of transposition cipher ref : https://en.wikipedia.org/wiki/Rail_fence_cipher
---
##### Functions:
1. [`Encrypt`](.cipher/railfence/railfence.go#L7): Encrypt encrypts a message using rail fence cipher
2. [`Decrypt`](.cipher/railfence/railfence.go#L44): decrypt decrypts a message using rail fence cipher
3. [`TestEncrypt`](.cipher/railfence/railfence_test.go#L7) Test function for Encrypt
4. [`TestDecrypt`](.cipher/railfence/railfence_test.go#L50) Test function for Decrypt
---
================================================
FILE: STYLE.md
================================================
# The Algorithms Go Repository Style Guide
This guide contains a set of go idioms and best practices which should be followed while writing
code whenever they are applicable. The instructions in this document should be given precedence
if there is a conflict between this document and [contribution guidelines](./CONTRIBUTING.md). If you find any issue or ambiguity in
this document, the maintainers of this repository should be consulted.
## Table of Contents
- [Formatting](#formatting)
- [Commenting](#commenting)
- [Package Comments](#package-comments)
- [Documentation Comments](#documentation-comments)
- [Author Comments](#author-comments)
- [Naming](#naming)
- [Package Naming](#package-naming)
- [Symbol Naming](#symbol-naming)
- [Function Naming](#function-naming)
- [Constructor Naming](#constructor-naming)
- [Getter Naming](#getter-naming)
- [Setter Naming](#setter-naming)
- [Interface Naming](#interface-naming)
## Formatting
All go code should be formatted with the official formatting tool `gofmt`. This requirement is
verified by the repository workflows.
## Commenting
All exported symbols should be commented with documentation, so that their motivation and use is
clear. All documentation should record any nuances of the symbol so that users are well aware of
them.
C++ style line comments should be preferred over C-style block comments.
| Bad | Good |
|
```go
/*
Unmarshal converts a JSON string into a go interface
...
*/
```
|
```go
// Unmarshal converts a JSON string into a go interface
// ...
```
|
### Package Comments
Package comments should start with the word "Package" followed by the package name. For packages
spanning multiple files or with a need for a large documentation, use a separate `doc.go` file for package level documentation comment.
| Bad | Good |
|
```go
// sort is a package which implements sorting functions.
```
|
```go
// Package sort implements sorting functions.
```
|
### Documentation Comments
All doc comments for symbols should start with the symbol name.
| Bad | Good |
|
```go
// Function Quick is an implementation
// of the Quicksort algorithm.
```
|
```go
// Quick is an implementation
// of the Quicksort algorithm.
```
|
### Author Comments
A comment recording the author of a particular file may also be added. This comment should be
written at the top of the file and it should not be a part of the package documentation.
| Bad | Good |
|
```go
// author: Rak Laptudirm (@raklaptudirm)
package sort
```
Comment is a part of the package documentation.
|
```go
// author: Rak Laptudirm (@raklaptudirm)
package sort
```
Comment is not a part of the package documentation.
|
## Naming
### Package Naming
Package names should be short and to the point. Keep in mind that this identifier will be used to
refer to your package, so make it easy to remember. It should be only composed of lower case
letters. Prefer `json` to `JSON` or `Json`. If your package name has two words, merge them
together. Prefer `jsonencoding` to `jsonEncoding` or `json_encoding`. Although, there is almost always a word to succinctly describe the package with an appropriate name. So if you have a name that describes the package nicely, please use that.
Add the `_test` suffix to your package name to implement black-box testing for your package in the test files.
### Symbol Naming
Go symbols should be named in the `camelCase` or `PascalCase`, depending of whether the symbol
is exported or not. The case when using acronyms in names should be consistent. Use `json` or
`JSON` instead of `Json`.
For exported symbols, use the package name to your advantage to write concise symbol names. For
example, if you have a package `png`, instead of defining a `png.PNGFile`, define a `png.File`.
It is much clearer and avoids repetition.
### Function Naming
#### Constructor Naming
Constructors should use the naming format `New()` for constructors which return pointers and
`Make()` for constructors which return values in accordance with the Go Programming Language's
`new` and `make` functions. If the package only exports a single constructor, use the name `New()`.
Some valid names are `reader.New()`, `metainfo.NewFile()`.
#### Getter Naming
Usage of Getters and Setters are discouraged. Use exported variables instead.
Getters should use the name of the field that is being fetched. For example, prefer a getter
name like `user.Name()` to `user.GetName()`.
#### Setter Naming
Setters should use names in the format `Set`. For example, `user.SetName()`.
### Interface Naming
Interfaces should use names in the format of `er` or ``. For example, some
valid interface names are:
```go
type Node interface {
Node()
}
type Writer interface {
Write()
}
```
================================================
FILE: cache/lfu.go
================================================
// lfu.go
// description: a type of cache algorithm used to manage memory within a computer.
// details:
// The standard characteristics of this method involve the system keeping track of the number of times a block is referenced in memory.
// When the cache is full and requires more room the system will purge the item with the lowest reference frequency.
// ref: (https://en.wikipedia.org/wiki/Least_frequently_used)
// time complexity: O(N)
// space complexity: O(1)
// author: [CocaineCong](https://github.com/CocaineCong)
package cache
import (
"container/list"
"math"
)
// LFU the Least Frequently Used (LFU) page-replacement algorithm
type LFU struct {
len int // length
cap int // capacity
minFreq int // The element that operates least frequently in LFU
// key: key of element, value: value of element
itemMap map[string]*list.Element
// key: frequency of possible occurrences of all elements in the itemMap
// value: elements with the same frequency
freqMap map[int]*list.List
}
// NewLFU init the LFU cache with capacity
func NewLFU(capacity int) LFU {
return LFU{
len: 0,
cap: capacity,
minFreq: math.MaxInt,
itemMap: make(map[string]*list.Element),
freqMap: make(map[int]*list.List),
}
}
// initItem to init item for LFU
func initItem(k string, v any, f int) item {
return item{
key: k,
value: v,
freq: f,
}
}
// Get the key in cache by LFU
func (c *LFU) Get(key string) any {
// if existed, will return value
if e, ok := c.itemMap[key]; ok {
// the frequency of e +1 and change freqMap
c.increaseFreq(e)
obj := e.Value.(item)
return obj.value
}
// if not existed, return nil
return nil
}
// Put the key in LFU cache
func (c *LFU) Put(key string, value any) {
if e, ok := c.itemMap[key]; ok {
// if key existed, update the value
obj := e.Value.(item)
obj.value = value
c.increaseFreq(e)
} else {
// if key not existed
obj := initItem(key, value, 1)
// if the length of item gets to the top line
// remove the least frequently operated element
if c.len == c.cap {
c.eliminate()
c.len--
}
// insert in freqMap and itemMap
c.insertMap(obj)
// change minFreq to 1 because insert the newest one
c.minFreq = 1
// length++
c.len++
}
}
// increaseFreq increase the frequency if element
func (c *LFU) increaseFreq(e *list.Element) {
obj := e.Value.(item)
// remove from low frequency first
oldLost := c.freqMap[obj.freq]
oldLost.Remove(e)
// change the value of minFreq
if c.minFreq == obj.freq && oldLost.Len() == 0 {
// if it is the last node of the minimum frequency that is removed
c.minFreq++
}
// add to high frequency list
c.insertMap(obj)
}
// insertMap insert item in map
func (c *LFU) insertMap(obj item) {
// add in freqMap
l, ok := c.freqMap[obj.freq]
if !ok {
l = list.New()
c.freqMap[obj.freq] = l
}
e := l.PushFront(obj)
// update or add the value of itemMap key to e
c.itemMap[obj.key] = e
}
// eliminate clear the least frequently operated element
func (c *LFU) eliminate() {
l := c.freqMap[c.minFreq]
e := l.Back()
obj := e.Value.(item)
l.Remove(e)
delete(c.itemMap, obj.key)
}
================================================
FILE: cache/lfu_test.go
================================================
package cache_test
import (
"testing"
"github.com/TheAlgorithms/Go/cache"
)
func TestLFU(t *testing.T) {
lfuCache := cache.NewLFU(3)
t.Run("Test 1: Put number and Get is correct", func(t *testing.T) {
key, value := "1", 1
lfuCache.Put(key, value)
got := lfuCache.Get(key)
if got != value {
t.Errorf("expected: %v, got: %v", value, got)
}
})
t.Run("Test 2: Get data not stored in cache should return nil", func(t *testing.T) {
got := lfuCache.Get("2")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("Test 3: Put data over capacity and Get should return nil", func(t *testing.T) {
lfuCache.Put("2", 2)
lfuCache.Put("3", 3)
lfuCache.Put("4", 4)
got := lfuCache.Get("1")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("test 4: Put key over capacity but recent key exists", func(t *testing.T) {
lfuCache.Put("4", 4)
lfuCache.Put("3", 3)
lfuCache.Put("2", 2)
lfuCache.Put("1", 1)
got := lfuCache.Get("4")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
expected := 3
got = lfuCache.Get("3")
if got != expected {
t.Errorf("expected: %v, got: %v", expected, got)
}
})
}
================================================
FILE: cache/lru.go
================================================
// lru.go
// description : Least Recently Used (LRU) cache
// details : A Least Recently Used (LRU) cache is a type of cache algorithm used to manage memory within a computer. The LRU algorithm is designed to remove the least recently used items first when the cache reaches its limit.
// time complexity : O(1)
// space complexity : O(n)
// ref : https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)
package cache
import (
"github.com/TheAlgorithms/Go/structure/linkedlist"
)
type item struct {
key string
value any
// the frequency of key
freq int
}
type LRU struct {
dl *linkedlist.Doubly[any]
size int
capacity int
storage map[string]*linkedlist.Node[any]
}
// NewLRU represent initiate lru cache with capacity
func NewLRU(capacity int) LRU {
return LRU{
dl: linkedlist.NewDoubly[any](),
storage: make(map[string]*linkedlist.Node[any], capacity),
size: 0,
capacity: capacity,
}
}
// Get value from lru
// if not found, return nil
func (c *LRU) Get(key string) any {
v, ok := c.storage[key]
if ok {
c.dl.MoveToBack(v)
return v.Val.(item).value
}
return nil
}
// Put cache with key and value to lru
func (c *LRU) Put(key string, value any) {
e, ok := c.storage[key]
if ok {
n := e.Val.(item)
n.value = value
e.Val = n
c.dl.MoveToBack(e)
return
}
if c.size >= c.capacity {
e := c.dl.Front()
dk := e.Val.(item).key
c.dl.Remove(e)
delete(c.storage, dk)
c.size--
}
n := item{key: key, value: value}
c.dl.AddAtEnd(n)
ne := c.dl.Back()
c.storage[key] = ne
c.size++
}
================================================
FILE: cache/lru_test.go
================================================
package cache_test
import (
"testing"
"github.com/TheAlgorithms/Go/cache"
)
func TestLRU(t *testing.T) {
cache := cache.NewLRU(3)
t.Run("Test 1: Put number and Get is correct", func(t *testing.T) {
key, value := "1", 1
cache.Put(key, value)
got := cache.Get(key)
if got != value {
t.Errorf("expected: %v, got: %v", value, got)
}
})
t.Run("Test 2: Get data not stored in cache should return nil", func(t *testing.T) {
got := cache.Get("2")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("Test 3: Put data over capacity and Get should return nil", func(t *testing.T) {
cache.Put("2", 2)
cache.Put("3", 3)
cache.Put("4", 4)
got := cache.Get("1")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
})
t.Run("test 4: Put key over capacity but recent key exists", func(t *testing.T) {
cache.Put("4", 4)
cache.Put("3", 3)
cache.Put("2", 2)
cache.Put("1", 1)
got := cache.Get("4")
if got != nil {
t.Errorf("expected: nil, got: %v", got)
}
expected := 3
got = cache.Get("3")
if got != expected {
t.Errorf("expected: %v, got: %v", expected, got)
}
})
}
================================================
FILE: checksum/crc8.go
================================================
// crc8.go
// description: Calculate CRC8
// details:
// A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks
// and storage devices to detect accidental changes to raw data.
// time complexity: O(n)
// space complexity: O(1)
// See more [CRC](https://en.wikipedia.org/wiki/Cyclic_redundancy_check)
// author(s) [red_byte](https://github.com/i-redbyte)
// see crc8_test.go
package checksum
import "math/bits"
// CRCModel contains the necessary parameters for calculating the DRC algorithm
type CRCModel struct {
Poly uint8
Init uint8
RefIn bool
RefOut bool
XorOut uint8
Name string
}
// CRC8 calculates CRC8 checksum of the given data.
func CRC8(data []byte, model CRCModel) uint8 {
table := getTable(model)
crcResult := model.Init
crcResult = addBytes(data, model, crcResult, table)
if model.RefOut {
crcResult = bits.Reverse8(crcResult)
}
return crcResult ^ model.XorOut
}
// This function get the result of adding the bytes in data to the crc
func addBytes(data []byte, model CRCModel, crcResult uint8, table []uint8) uint8 {
if model.RefIn {
for _, d := range data {
d = bits.Reverse8(d)
crcResult = table[crcResult^d]
}
return crcResult
}
for _, d := range data {
crcResult = table[crcResult^d]
}
return crcResult
}
// This function get 256-byte (256x8) table for efficient processing.
func getTable(model CRCModel) []uint8 {
table := make([]uint8, 256)
for i := 0; i < 256; i++ {
crc := uint8(i)
for j := 0; j < 8; j++ {
isSetBit := (crc & 0x80) != 0
crc <<= 1
if isSetBit {
crc ^= model.Poly
}
}
table[i] = crc
}
return table
}
================================================
FILE: checksum/crc8_test.go
================================================
// crc8_test.go
// description: Test for calculate crc8
// author(s) [red_byte](https://github.com/i-redbyte)
// see crc8.go
package checksum_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/checksum"
)
var (
CRC8 = checksum.CRCModel{0x07, 0x00, false, false, 0x00, "CRC-8"}
CRC8CDMA2000 = checksum.CRCModel{0x9B, 0xFF, false, false, 0x00, "CRC-8/CDMA2000"}
CRC8DARC = checksum.CRCModel{0x39, 0x00, true, true, 0x00, "CRC-8/DARC"}
CRC8DVBS2 = checksum.CRCModel{0xD5, 0x00, false, false, 0x00, "CRC-8/DVB-S2"}
CRC8EBU = checksum.CRCModel{0x1D, 0xFF, true, true, 0x00, "CRC-8/EBU"}
CRC8ICODE = checksum.CRCModel{0x1D, 0xFD, false, false, 0x00, "CRC-8/I-CODE"}
CRC8ITU = checksum.CRCModel{0x07, 0x00, false, false, 0x55, "CRC-8/ITU"}
CRC8MAXIM = checksum.CRCModel{0x31, 0x00, true, true, 0x00, "CRC-8/MAXIM"}
CRC8ROHC = checksum.CRCModel{0x07, 0xFF, true, true, 0x00, "CRC-8/ROHC"}
CRC8WCDMA = checksum.CRCModel{0x9B, 0x00, true, true, 0x00, "CRC-8/WCDMA"}
)
func TestCalculateCRC8(t *testing.T) {
data := []byte("123456789")
tests := []struct {
name string
data []byte
model checksum.CRCModel
want uint8
}{
{CRC8.Name, data, CRC8, 0xF4},
{CRC8CDMA2000.Name, data, CRC8CDMA2000, 0xDA},
{CRC8DARC.Name, data, CRC8DARC, 0x15},
{CRC8DVBS2.Name, data, CRC8DVBS2, 0xBC},
{CRC8EBU.Name, data, CRC8EBU, 0x97},
{CRC8ICODE.Name, data, CRC8ICODE, 0x7E},
{CRC8ITU.Name, data, CRC8ITU, 0xA1},
{CRC8MAXIM.Name, data, CRC8MAXIM, 0xA1},
{CRC8ROHC.Name, data, CRC8ROHC, 0xD0},
{CRC8WCDMA.Name, data, CRC8WCDMA, 0x25},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fmt.Println(checksum.CRC8(test.data, test.model))
if got := checksum.CRC8(test.data, test.model); got != test.want {
t.Errorf("CalculateCRC8() = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkCalculateCRC8(b *testing.B) {
for i := 0; i < b.N; i++ {
checksum.CRC8([]byte("123456789"), CRC8)
}
}
================================================
FILE: checksum/luhn.go
================================================
// lunh.go
// description: Luhn algorithm
// details: is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, etc [Lunh](https://en.wikipedia.org/wiki/Luhn_algorithm)
// time complexity: O(n)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see lunh_test.go
// Package checksum describes algorithms for finding various checksums
package checksum
// Luhn validates the provided data using the Luhn algorithm.
func Luhn(s []byte) bool {
n := len(s)
number := 0
result := 0
for i := 0; i < n; i++ {
number = int(s[i]) - '0'
if i%2 != 0 {
result += number
continue
}
number *= 2
if number > 9 {
number -= 9
}
result += number
}
return result%10 == 0
}
================================================
FILE: checksum/luhn_test.go
================================================
// luhn_test.go
// description: Test for Luhn algorithm
// author(s) [red_byte](https://github.com/i-redbyte)
// see luhn.go
package checksum_test
import (
"testing"
"github.com/TheAlgorithms/Go/checksum"
)
func TestLuhn(t *testing.T) {
tests := []struct {
name string
s []byte
want bool
}{
{"check 4242424242424242", []byte("4242424242424242"), true},
{"check 6200000000000005", []byte("6200000000000005"), true},
{"check 5534200028533164", []byte("5534200028533164"), true},
{"check 36227206271667", []byte("36227206271667"), true},
{"check 471629309440", []byte("471629309440"), false},
{"check 1111", []byte("1111"), false},
{"check 12345674", []byte("12345674"), true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := checksum.Luhn(test.s); got != test.want {
t.Errorf("LuhnAlgorithm() = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkBruteForceFactorial(b *testing.B) {
for i := 0; i < b.N; i++ {
checksum.Luhn([]byte("4242424242424242"))
}
}
================================================
FILE: cipher/caesar/caesar.go
================================================
// Package caesar is the shift cipher
// description: Caesar cipher
// details : Caesar cipher is a type of substitution cipher in which each letter in the plaintext is shifted a certain number of places down the alphabet.
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/Caesar_cipher
package caesar
// Encrypt encrypts by right shift of "key" each character of "input"
func Encrypt(input string, key int) string {
// if key is negative value,
// updates "key" the number which congruents to "key" modulo 26
key8 := byte(key%26+26) % 26
var outputBuffer []byte
// b is a byte, which is the equivalent of uint8.
for _, b := range []byte(input) {
newByte := b
if 'A' <= b && b <= 'Z' {
outputBuffer = append(outputBuffer, 'A'+(newByte-'A'+key8)%26)
} else if 'a' <= b && b <= 'z' {
outputBuffer = append(outputBuffer, 'a'+(newByte-'a'+key8)%26)
} else {
outputBuffer = append(outputBuffer, newByte)
}
}
return string(outputBuffer)
}
// Decrypt decrypts by left shift of "key" each character of "input"
func Decrypt(input string, key int) string {
// left shift of "key" is same as right shift of 26-"key"
return Encrypt(input, 26-key)
}
================================================
FILE: cipher/caesar/caesar_test.go
================================================
package caesar
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestEncrypt(t *testing.T) {
var caesarTestData = []struct {
description string
input string
key int
expected string
}{
{
"Basic caesar encryption with letter 'a'",
"a",
3,
"d",
},
{
"Basic caesar encryption wrap around alphabet on letter 'z'",
"z",
3,
"c",
},
{
"Encrypt a simple string with caesar encryiption",
"hello",
3,
"khoor",
},
{
"Encrypt a simple string with key 13",
"hello",
13,
"uryyb",
},
{
"Encrypt a simple string with key -13",
"hello",
-13,
"uryyb",
},
{
"With key of 26 output should be the same as the input",
"no change",
26,
"no change",
},
{
"Encrypt sentence with key 10",
"the quick brown fox jumps over the lazy dog.",
10,
"dro aesmu lbygx pyh tewzc yfob dro vkji nyq.",
},
{
"Encrypt sentence with key 10",
"The Quick Brown Fox Jumps over the Lazy Dog.",
10,
"Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq.",
},
}
for _, test := range caesarTestData {
t.Run(test.description, func(t *testing.T) {
actual := Encrypt(test.input, test.key)
if actual != test.expected {
t.Logf("FAIL: %s", test.description)
t.Fatalf("With input string '%s' and key '%d' was expecting '%s' but actual was '%s'",
test.input, test.key, test.expected, actual)
}
})
}
}
func TestDecrypt(t *testing.T) {
var caesarTestData = []struct {
description string
input string
key int
expected string
}{
{
"Basic caesar decryption with letter 'a'",
"a",
3,
"x",
},
{
"Basic caesar decryption wrap around alphabet on letter 'z'",
"z",
3,
"w",
},
{
"Decrypt a simple string with caesar encryiption",
"hello",
3,
"ebiil",
},
{
"Decrypt a simple string with key 13",
"hello",
13,
"uryyb",
},
{
"Decrypt a simple string with key -13",
"hello",
-13,
"uryyb",
},
{
"With key of 26 output should be the same as the input",
"no change",
26,
"no change",
},
{
"Decrypt sentence with key 10",
"Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq.",
10,
"The Quick Brown Fox Jumps over the Lazy Dog.",
},
}
for _, test := range caesarTestData {
t.Run(test.description, func(t *testing.T) {
actual := Decrypt(test.input, test.key)
if actual != test.expected {
t.Logf("FAIL: %s", test.description)
t.Fatalf("With input string '%s' and key '%d' was expecting '%s' but actual was '%s'",
test.input, test.key, test.expected, actual)
}
})
}
}
func Example() {
const (
key = 10
input = "The Quick Brown Fox Jumps over the Lazy Dog."
)
encryptedText := Encrypt(input, key)
fmt.Printf("Encrypt=> key: %d, input: %s, encryptedText: %s\n", key, input, encryptedText)
decryptedText := Decrypt(encryptedText, key)
fmt.Printf("Decrypt=> key: %d, input: %s, decryptedText: %s\n", key, encryptedText, decryptedText)
// Output:
// Encrypt=> key: 10, input: The Quick Brown Fox Jumps over the Lazy Dog., encryptedText: Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq.
// Decrypt=> key: 10, input: Dro Aesmu Lbygx Pyh Tewzc yfob dro Vkji Nyq., decryptedText: The Quick Brown Fox Jumps over the Lazy Dog.
}
func FuzzCaesar(f *testing.F) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
f.Add("The Quick Brown Fox Jumps over the Lazy Dog.")
f.Fuzz(func(t *testing.T, input string) {
key := rnd.Intn(26)
if result := Decrypt(Encrypt(input, key), key); result != input {
t.Fatalf("With input: '%s' and key: %d\n\tExpected: '%s'\n\tGot: '%s'", input, key, input, result)
}
})
}
================================================
FILE: cipher/cipher_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package cipher
================================================
FILE: cipher/diffiehellman/diffiehellmankeyexchange.go
================================================
// Package diffiehellman implements Diffie-Hellman Key Exchange Algorithm
// description: Diffie-Hellman key exchange
// details : Diffie-Hellman key exchange is a method of securely exchanging cryptographic keys over a public channel by combining private keys of two parties to generate a shared secret key.
// time complexity: O(log(n))
// space complexity: O(1)
// for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI
package diffiehellman
const (
generator = 3
primeNumber int64 = 6700417 // prime number discovered by Leonhard Euler
)
// GenerateShareKey : generates a key using client private key , generator and primeNumber
// this key can be made public
// shareKey = (g^key)%primeNumber
func GenerateShareKey(prvKey int64) int64 {
return modularExponentiation(generator, prvKey, primeNumber)
}
// GenerateMutualKey : generates a mutual key that can be used by only alice and bob
// mutualKey = (shareKey^prvKey)%primeNumber
func GenerateMutualKey(prvKey, shareKey int64) int64 {
return modularExponentiation(shareKey, prvKey, primeNumber)
}
// r = (b^e)%mod
func modularExponentiation(b, e, mod int64) int64 {
//runs in O(log(n)) where n = e
//uses exponentiation by squaring to speed up the process
if mod == 1 {
return 0
}
var r int64 = 1
b = b % mod
for e > 0 {
if e&1 == 1 {
r = (r * b) % mod
}
e = e >> 1
b = (b * b) % mod
}
return r
}
================================================
FILE: cipher/diffiehellman/diffiehellmankeyexchange_test.go
================================================
package diffiehellman
import (
"crypto/rand"
"crypto/rsa"
"testing"
)
func TestDiffieHellmanKeyExchange(t *testing.T) {
t.Run("Test 1: modularExponentiation", func(t *testing.T) {
var want int64 = 9 // (3^5)mod13 = 243mod13 = 9
var prvKey int64 = 5
var generator int64 = 3
var primeNumber int64 = 13
got := modularExponentiation(generator, prvKey, primeNumber)
if got != want {
t.Errorf(`with privateKey=%d, generator=%d and primeNumber=%d
modularExponentiation should result=%d, but resulted=%d`, prvKey, generator, primeNumber, want, got)
}
})
t.Run("Test 2: Key Exchange", func(t *testing.T) {
// generating a small sized rsa_cipher key for testing
alicePrvKey, _ := rsa.GenerateKey(rand.Reader, 31)
bobPrvKey, _ := rsa.GenerateKey(rand.Reader, 31)
// alice and bob generates their respective share key with their privateKey
shareKeyByAlice := GenerateShareKey(alicePrvKey.D.Int64())
shareKeyByBob := GenerateShareKey(bobPrvKey.D.Int64())
// generated share key now can be exchanged even via insecure channel
// mutualKey can be computed using shared key
mutualKeyComputedByAlice := GenerateMutualKey(alicePrvKey.D.Int64(), shareKeyByBob)
mutualKeyComputedByBob := GenerateMutualKey(bobPrvKey.D.Int64(), shareKeyByAlice)
if mutualKeyComputedByAlice != mutualKeyComputedByBob {
t.Errorf("mutual key computed by alice and bob should be same, but got un-equal")
}
})
}
================================================
FILE: cipher/doc.go
================================================
// Package cipher is a package containing different implementations of certain ciphers
package cipher
================================================
FILE: cipher/dsa/dsa.go
================================================
/*
dsa.go
description: DSA encryption and decryption including key generation
details: [DSA wiki](https://en.wikipedia.org/wiki/Digital_Signature_Algorithm)
author(s): [ddaniel27](https://github.com/ddaniel27)
*/
package dsa
import (
"crypto/rand"
"io"
"math/big"
)
const (
numMRTests = 64 // Number of Miller-Rabin tests
L = 1024 // Number of bits in p
N = 160 // Number of bits in q
)
type (
// parameters represents the DSA parameters
parameters struct {
P, Q, G *big.Int
}
// dsa represents the DSA
dsa struct {
parameters
pubKey *big.Int // public key (y)
privKey *big.Int // private key (x)
}
)
// New creates a new DSA instance
func New() *dsa {
d := new(dsa)
d.dsaParameterGeneration()
d.keyGen()
return d
}
// Parameter generation for DSA
// 1. FIPS 186-4 specifies that the L and N values must be (1024, 160), (2048, 224), or (3072, 256)
// 2. Choose a N-bit prime q
// 3. Choose a L-bit prime p such that p-1 is a multiple of q
// 4. Choose an integer h randomly from the range [2, p-2]
// 5. Compute g = h^((p-1)/q) mod p
// 6. Return (p, q, g)
func (dsa *dsa) dsaParameterGeneration() {
var err error
p, q, bigInt := new(big.Int), new(big.Int), new(big.Int)
one, g, h := big.NewInt(1), big.NewInt(1), big.NewInt(2)
pBytes := make([]byte, L/8)
// GPLoop is a label for the loop
// We use this loop to change the prime q if we don't find a prime p
GPLoop:
for {
// 2. Choose a N-bit prime q
q, err = rand.Prime(rand.Reader, N)
if err != nil {
panic(err)
}
for i := 0; i < 4*L; i++ {
// 3. Choose a L-bit prime p such that p-1 is a multiple of q
// In this case we generate a random number of L bits
if _, err := io.ReadFull(rand.Reader, pBytes); err != nil {
panic(err)
}
// This are the minimum conditions for p being a possible prime
pBytes[len(pBytes)-1] |= 1 // p is odd
pBytes[0] |= 0x80 // p has the highest bit set
p.SetBytes(pBytes)
// Instead of using (p-1)%q == 0
// We ensure that p-1 is a multiple of q and validates if p is prime
bigInt.Mod(p, q)
bigInt.Sub(bigInt, one)
p.Sub(p, bigInt)
if p.BitLen() < L || !p.ProbablyPrime(numMRTests) { // Check if p is prime and has L bits
continue
}
dsa.P = p
dsa.Q = q
break GPLoop
}
}
// 4. Choose an integer h randomly from the range [2, p-2]. Commonly, h = 2
// 5. Compute g = h^((p-1)/q) mod p. In case g == 1, increment h until g != 1
pm1 := new(big.Int).Sub(p, one)
for g.Cmp(one) == 0 {
g.Exp(h, new(big.Int).Div(pm1, q), p)
h.Add(h, one)
}
dsa.G = g
}
// keyGen is key generation for DSA
// 1. Choose a random integer x from the range [1, q-1]
// 2. Compute y = g^x mod p
func (dsa *dsa) keyGen() {
// 1. Choose a random integer x from the range [1, q-1]
x, err := rand.Int(rand.Reader, new(big.Int).Sub(dsa.Q, big.NewInt(1)))
if err != nil {
panic(err)
}
dsa.privKey = x
// 2. Compute y = g^x mod p
dsa.pubKey = new(big.Int).Exp(dsa.G, x, dsa.P)
}
// Sign is signature generation for DSA
// 1. Choose a random integer k from the range [1, q-1]
// 2. Compute r = (g^k mod p) mod q
// 3. Compute s = (k^-1 * (H(m) + x*r)) mod q
func Sign(m []byte, p, q, g, x *big.Int) (r, s *big.Int) {
// 1. Choose a random integer k from the range [1, q-1]
k, err := rand.Int(rand.Reader, new(big.Int).Sub(q, big.NewInt(1)))
if err != nil {
panic(err)
}
// 2. Compute r = (g^k mod p) mod q
r = new(big.Int).Exp(g, k, p)
r.Mod(r, q)
// 3. Compute s = (k^-1 * (H(m) + x*r)) mod q
h := new(big.Int).SetBytes(m) // This should be the hash of the message
s = new(big.Int).ModInverse(k, q) // k^-1 mod q
s.Mul(
s,
new(big.Int).Add( // (H(m) + x*r)
h,
new(big.Int).Mul(x, r),
),
)
s.Mod(s, q) // mod q
return r, s
}
// Verify is signature verification for DSA
// 1. Compute w = s^-1 mod q
// 2. Compute u1 = (H(m) * w) mod q
// 3. Compute u2 = (r * w) mod q
// 4. Compute v = ((g^u1 * y^u2) mod p) mod q
// 5. If v == r, the signature is valid
func Verify(m []byte, r, s, p, q, g, y *big.Int) bool {
// 1. Compute w = s^-1 mod q
w := new(big.Int).ModInverse(s, q)
// 2. Compute u1 = (H(m) * w) mod q
h := new(big.Int).SetBytes(m) // This should be the hash of the message
u1 := new(big.Int).Mul(h, w)
u1.Mod(u1, q)
// 3. Compute u2 = (r * w) mod q
u2 := new(big.Int).Mul(r, w)
u2.Mod(u2, q)
// 4. Compute v = ((g^u1 * y^u2) mod p) mod q
v := new(big.Int).Exp(g, u1, p)
v.Mul(
v,
new(big.Int).Exp(y, u2, p),
)
v.Mod(v, p)
v.Mod(v, q)
// 5. If v == r, the signature is valid
return v.Cmp(r) == 0
}
// GetPublicKey returns the public key (y)
func (dsa *dsa) GetPublicKey() *big.Int {
return dsa.pubKey
}
// GetParameters returns the DSA parameters (p, q, g)
func (dsa *dsa) GetParameters() parameters {
return dsa.parameters
}
// GetPrivateKey returns the private Key (x)
func (dsa *dsa) GetPrivateKey() *big.Int {
return dsa.privKey
}
================================================
FILE: cipher/dsa/dsa_test.go
================================================
package dsa_test
import (
"math/big"
"testing"
"github.com/TheAlgorithms/Go/cipher/dsa"
)
func TestDSA(t *testing.T) {
tests := []struct {
name string
message string
alter bool
want bool
}{
{
name: "valid signature",
message: "Hello, world!",
alter: false,
want: true,
},
{
name: "invalid signature",
message: "Hello, world!",
alter: true,
want: false,
},
}
// Generate a DSA key pair
dsaInstance := dsa.New()
pubKey := dsaInstance.GetPublicKey()
params := dsaInstance.GetParameters()
privKey := dsaInstance.GetPrivateKey()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Sign the message
r, s := dsa.Sign(
[]byte(tt.message),
params.P,
params.Q,
params.G,
privKey,
)
if tt.alter {
// Alter the signature
r.Add(r, big.NewInt(1))
}
// Verify the signature
if got := dsa.Verify(
[]byte(tt.message),
r,
s,
params.P,
params.Q,
params.G,
pubKey,
); got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
/* ------------------- BENCHMARKS ------------------- */
func BenchmarkDSANew(b *testing.B) {
for i := 0; i < b.N; i++ {
dsa.New()
}
}
func BenchmarkDSASign(b *testing.B) {
dsaInstance := dsa.New()
params := dsaInstance.GetParameters()
privKey := dsaInstance.GetPrivateKey()
for i := 0; i < b.N; i++ {
dsa.Sign(
[]byte("Hello, World!"),
params.P,
params.Q,
params.G,
privKey,
)
}
}
func BenchmarkDSAVerify(b *testing.B) {
dsaInstance := dsa.New()
pubKey := dsaInstance.GetPublicKey()
params := dsaInstance.GetParameters()
privKey := dsaInstance.GetPrivateKey()
r, s := dsa.Sign(
[]byte("Hello, World!"),
params.P,
params.Q,
params.G,
privKey,
)
for i := 0; i < b.N; i++ {
dsa.Verify(
[]byte("Hello, World!"),
r,
s,
params.P,
params.Q,
params.G,
pubKey,
)
}
}
================================================
FILE: cipher/polybius/polybius.go
================================================
// Package polybius is encrypting method with polybius square
// description: Polybius square
// details : The Polybius algorithm is a simple algorithm that is used to encode a message by converting each letter to a pair of numbers.
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/Polybius_square#Hybrid_Polybius_Playfair_Cipher
package polybius
import (
"fmt"
"math"
"strings"
)
// Polybius is struct having size, characters, and key
type Polybius struct {
size int
characters string
key string
}
// NewPolybius returns a pointer to object of Polybius.
// If the size of "chars" is longer than "size",
// "chars" are truncated to "size".
func NewPolybius(key string, size int, chars string) (*Polybius, error) {
if size < 0 {
return nil, fmt.Errorf("provided size %d cannot be negative", size)
}
key = strings.ToUpper(key)
if size > len(chars) {
return nil, fmt.Errorf("provided size %d is too small to use to slice string %q of len %d", size, chars, len(chars))
}
for _, r := range chars {
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
return nil, fmt.Errorf("provided string %q should only contain latin characters", chars)
}
}
chars = strings.ToUpper(chars)[:size]
for i, r := range chars {
if strings.ContainsRune(chars[i+1:], r) {
return nil, fmt.Errorf("%q contains same character %q", chars[i+1:], r)
}
}
if len(key) != size*size {
return nil, fmt.Errorf("len(key): %d must be as long as size squared: %d", len(key), size*size)
}
return &Polybius{size, chars, key}, nil
}
// Encrypt encrypts with polybius encryption
func (p *Polybius) Encrypt(text string) (string, error) {
encryptedText := ""
for _, char := range strings.ToUpper(text) {
encryptedChar, err := p.encipher(char)
if err != nil {
return "", fmt.Errorf("failed encipher: %w", err)
}
encryptedText += encryptedChar
}
return encryptedText, nil
}
// Decrypt decrypts with polybius encryption
func (p *Polybius) Decrypt(text string) (string, error) {
chars := []rune(strings.ToUpper(text))
decryptedText := ""
for i := 0; i < len(chars); i += 2 {
decryptedChar, err := p.decipher(chars[i:int(math.Min(float64(i+2), float64(len(chars))))])
if err != nil {
return "", fmt.Errorf("failed decipher: %w", err)
}
decryptedText += decryptedChar
}
return decryptedText, nil
}
func (p *Polybius) encipher(char rune) (string, error) {
index := strings.IndexRune(p.key, char)
if index < 0 {
return "", fmt.Errorf("%q does not exist in keys", char)
}
row := index / p.size
col := index % p.size
chars := []rune(p.characters)
return string([]rune{chars[row], chars[col]}), nil
}
func (p *Polybius) decipher(chars []rune) (string, error) {
if len(chars) != 2 {
return "", fmt.Errorf("the size of \"chars\" must be even")
}
row := strings.IndexRune(p.characters, chars[0])
if row < 0 {
return "", fmt.Errorf("%c does not exist in characters", chars[0])
}
col := strings.IndexRune(p.characters, chars[1])
if col < 0 {
return "", fmt.Errorf("%c does not exist in characters", chars[1])
}
return string(p.key[row*p.size+col]), nil
}
================================================
FILE: cipher/polybius/polybius_test.go
================================================
package polybius
import (
"fmt"
"log"
"strings"
"testing"
)
func ExampleNewPolybius() {
// initialize
const (
plainText = "HogeFugaPiyoSpam"
size = 5
characters = "HogeF"
key = "abcdefghijklmnopqrstuvwxy"
)
p, err := NewPolybius(key, size, characters)
if err != nil {
log.Fatalf("failed NewPolybius: %v", err)
}
encryptedText, err := p.Encrypt(plainText)
if err != nil {
log.Fatalf("failed Encrypt: %v", err)
}
fmt.Printf("Encrypt=> plainText: %s, encryptedText: %s\n", plainText, encryptedText)
decryptedText, err := p.Decrypt(encryptedText)
if err != nil {
log.Fatalf("failed Decrypt: %v", err)
}
fmt.Printf("Decrypt=> encryptedText: %s, decryptedText: %s\n", encryptedText, decryptedText)
// Output:
// Encrypt=> plainText: HogeFugaPiyoSpam, encryptedText: OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG
// Decrypt=> encryptedText: OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG, decryptedText: HOGEFUGAPIYOSPAM
}
func TestNewPolybius(t *testing.T) {
t.Parallel()
cases := []struct {
name string
size int
characters string
key string
wantErr string
}{
{
name: "correct initialization", size: 5, characters: "HogeF", key: "abcdefghijklmnopqrstuvwxy", wantErr: "",
},
{
name: "truncate characters", size: 5, characters: "HogeFuga", key: "abcdefghijklmnopqrstuvwxy", wantErr: "",
},
{
name: "invalid key", size: 5, characters: "HogeFuga", key: "abcdefghi", wantErr: "len(key): 9 must be as long as size squared: 25",
},
{
name: "invalid characters", size: 5, characters: "HogeH", key: "abcdefghijklmnopqrstuvwxy", wantErr: "\"OGEH\" contains same character 'H'",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewPolybius(tc.key, tc.size, tc.characters)
if err != nil && err.Error() != tc.wantErr {
t.Errorf("failed NewPolybius: %v", err)
}
})
}
}
func TestPolybiusEncrypt(t *testing.T) {
t.Parallel()
cases := []struct {
name string
text string
want string
}{
{
name: "correct encryption", text: "HogeFugaPiyoSpam", want: "OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG",
},
{
name: "invalid encryption", text: "hogz", want: "failed encipher: 'Z' does not exist in keys",
},
}
// initialize
const (
size = 5
characters = "HogeF"
key = "abcdefghijklmnopqrstuvwxy"
)
p, err := NewPolybius(key, size, characters)
if err != nil {
t.Fatalf("failed NewPolybius: %v", err)
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
encrypted, err := p.Encrypt(tc.text)
if err != nil {
if err.Error() != tc.want {
t.Errorf("failed Encrypt: %v", err)
}
} else if encrypted != tc.want {
t.Errorf("Encrypt: %v, want: %v", encrypted, tc.want)
}
})
}
}
func TestPolybiusDecrypt(t *testing.T) {
t.Parallel()
cases := []struct {
name string
text string
want string
}{
{
name: "correct decryption", text: "OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG", want: "HOGEFUGAPIYOSPAM",
},
{
name: "invalid decryption(position of even number)", text: "hogz", want: "failed decipher: Z does not exist in characters",
},
{
name: "invalid decryption(position of odd number)", text: "hode", want: "failed decipher: D does not exist in characters",
},
{
name: "invalid text size which is odd", text: "hog", want: "failed decipher: the size of \"chars\" must be even",
},
}
// initialize
const (
size = 5
characters = "HogeF"
key = "abcdefghijklmnopqrstuvwxy"
)
p, err := NewPolybius(key, size, characters)
if err != nil {
t.Fatalf("failed NewPolybius: %v", err)
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
encrypted, err := p.Decrypt(tc.text)
if err != nil {
if err.Error() != tc.want {
t.Errorf("failed Encrypt: %v", err)
}
} else if encrypted != tc.want {
t.Errorf("Encrypt: %v, want: %v", encrypted, tc.want)
}
})
}
}
func FuzzPolybius(f *testing.F) {
const (
size = 5
characters = "HogeF"
key = "abcdefghijklmnopqrstuvwxy"
)
f.Add(size, characters, key)
f.Fuzz(func(t *testing.T, size int, characters, key string) {
p, err := NewPolybius(key, size, characters)
switch {
case err == nil:
case strings.Contains(err.Error(), "cannot be negative"),
strings.Contains(err.Error(), "is too small"),
strings.Contains(err.Error(), "should only contain latin characters"),
strings.Contains(err.Error(), "contains same character"),
strings.Contains(err.Error(), "must be as long as size squared"):
return
default:
t.Fatalf("unexpected error when creating a new polybius variable: %v", err)
}
encrypted, err := p.Encrypt(characters)
switch {
case err == nil:
case strings.Contains(err.Error(), "does not exist in keys"):
return
default:
t.Fatalf("unexpected error during encryption: %v", err)
}
decrypted, err := p.Decrypt(encrypted)
if err != nil {
t.Fatalf("unexpected error during decryption: %v", err)
}
if decrypted != strings.ToUpper(characters) {
t.Errorf("Expecting output to match with %q but was %q", characters, decrypted)
}
})
}
================================================
FILE: cipher/railfence/railfence.go
================================================
// railfence.go
// description: Rail Fence Cipher
// details: The rail fence cipher is a an encryption algorithm that uses a rail fence pattern to encode a message. it is a type of transposition cipher that rearranges the characters of the plaintext to form the ciphertext.
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/Rail_fence_cipher
package railfence
import (
"strings"
)
func Encrypt(text string, rails int) string {
if rails == 1 {
return text
}
// Create a matrix for the rail fence pattern
matrix := make([][]rune, rails)
for i := range matrix {
matrix[i] = make([]rune, len(text))
}
// Fill the matrix
dirDown := false
row, col := 0, 0
for _, char := range text {
if row == 0 || row == rails-1 {
dirDown = !dirDown
}
matrix[row][col] = char
col++
if dirDown {
row++
} else {
row--
}
}
var result strings.Builder
for _, line := range matrix {
for _, char := range line {
if char != 0 {
result.WriteRune(char)
}
}
}
return result.String()
}
func Decrypt(cipherText string, rails int) string {
if rails == 1 || rails >= len(cipherText) {
return cipherText
}
// Placeholder for the decrypted message
decrypted := make([]rune, len(cipherText))
// Calculate the zigzag pattern and place characters accordingly
index := 0
for rail := 0; rail < rails; rail++ {
position := rail
down := true // Direction flag
for position < len(cipherText) {
decrypted[position] = rune(cipherText[index])
index++
// Determine the next position based on the current rail and direction
if rail == 0 || rail == rails-1 {
position += 2 * (rails - 1)
} else if down {
position += 2 * (rails - 1 - rail)
down = false
} else {
position += 2 * rail
down = true
}
}
}
return string(decrypted)
}
================================================
FILE: cipher/railfence/railfence_test.go
================================================
package railfence
import (
"testing"
)
func TestEncrypt(t *testing.T) {
var railFenceTestData = []struct {
description string
input string
rails int
expected string
}{
{
"Encrypt with 2 rails",
"hello",
2,
"hloel",
},
{
"Encrypt with 3 rails",
"hello world",
3,
"horel ollwd",
},
{
"Encrypt with edge case: 1 rail",
"hello",
1,
"hello",
},
{
"Encrypt with more rails than letters",
"hi",
100,
"hi",
},
}
for _, test := range railFenceTestData {
t.Run(test.description, func(t *testing.T) {
actual := Encrypt(test.input, test.rails)
if actual != test.expected {
t.Errorf("FAIL: %s - Encrypt(%s, %d) = %s, want %s", test.description, test.input, test.rails, actual, test.expected)
}
})
}
}
func TestDecrypt(t *testing.T) {
var railFenceTestData = []struct {
description string
input string
rails int
expected string
}{
{
"Decrypt with 2 rails",
"hloel",
2,
"hello",
},
{
"Decrypt with 3 rails",
"ho l lewrdlo",
3,
"hld olle wor",
},
{
"Decrypt with edge case: 1 rail",
"hello",
1,
"hello",
},
{
"Decrypt with more rails than letters",
"hi",
100,
"hi",
},
}
for _, test := range railFenceTestData {
t.Run(test.description, func(t *testing.T) {
actual := Decrypt(test.input, test.rails)
if actual != test.expected {
t.Errorf("FAIL: %s - Decrypt(%s, %d) = %s, want %s", test.description, test.input, test.rails, actual, test.expected)
}
})
}
}
================================================
FILE: cipher/rot13/rot13.go
================================================
// Package rot13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet.
// description: ROT13
// details: ROT13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. it is a special case of the Caesar cipher
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/ROT13
package rot13
import (
"github.com/TheAlgorithms/Go/cipher/caesar"
)
// rot13 is a special case, which is fixed the shift of 13, of the Caesar cipher
func rot13(input string) string {
return caesar.Encrypt(input, 13)
}
================================================
FILE: cipher/rot13/rot13_test.go
================================================
package rot13
import (
"testing"
)
var rot13TestData = []struct {
description string
input string
expected string
}{
{
"Basic rotation with letter 'a' gives 'n",
"a",
"n",
},
{
"Rotation with wrapping around alphabet on letter 'z' gives 'm'",
"z",
"m",
},
{
"Rotation on 'hello world'",
"hello world",
"uryyb jbeyq",
},
{
"Rotation on the rotation of 'hello world' gives 'hello world' back",
"uryyb jbeyq",
"hello world",
},
{
"Full sentence rotation",
"the quick brown fox jumps over the lazy dog.",
"gur dhvpx oebja sbk whzcf bire gur ynml qbt.",
},
{
"Sentence from Rot13.go main function",
"we'll just make him an offer he can't refuse... tell me you get the pop culture reference",
"jr'yy whfg znxr uvz na bssre ur pna'g ershfr... gryy zr lbh trg gur cbc phygher ersrerapr",
},
}
func TestRot13Encrypt(t *testing.T) {
for _, test := range rot13TestData {
t.Run(test.description, func(t *testing.T) {
input := test.input
expected := test.expected
assertRot13Output(t, input, expected)
})
}
}
func TestRot13Decrypt(t *testing.T) {
for _, test := range rot13TestData {
t.Run(test.description, func(t *testing.T) {
input := test.expected
expected := test.input
assertRot13Output(t, input, expected)
})
}
}
func assertRot13Output(t *testing.T, input, expected string) {
actual := rot13(input)
if actual != expected {
t.Fatalf("With input string %q was expecting %q but actual was %q",
input, expected, actual)
}
}
func FuzzRot13(f *testing.F) {
for _, rot13TestInput := range rot13TestData {
f.Add(rot13TestInput.input)
}
f.Fuzz(func(t *testing.T, input string) {
if result := rot13(rot13(input)); result != input {
t.Fatalf("With input string %q was expecting %q but actual was %q",
input, input, result)
}
})
}
================================================
FILE: cipher/rsa/rsa.go
================================================
// rsa.go
// description: Simple RSA algorithm implementation
// details:
// A simple RSA Encryption and Decryption algorithm.
// It uses prime numbers that fit in int64 datatypes and
// thus both the Encrypt and Decrypt are not a production
// ready implementation. The OpenSSL implementation of RSA
// also adds a padding which is not present in this algorithm.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [Taj](https://github.com/tjgurwara99)
// see rsa_test.go
// Package rsa shows a simple implementation of RSA algorithm
package rsa
import (
"errors"
modular "github.com/TheAlgorithms/Go/math/modular"
)
// ErrorFailedToEncrypt Raised when Encrypt function fails to encrypt the message
var ErrorFailedToEncrypt = errors.New("failed to Encrypt")
// ErrorFailedToDecrypt Raised when Decrypt function fails to decrypt the encrypted message
var ErrorFailedToDecrypt = errors.New("failed to Decrypt")
// Encrypt encrypts based on the RSA algorithm - uses modular exponentitation in math directory
func Encrypt(message []rune, publicExponent, modulus int64) ([]rune, error) {
var encrypted []rune
for _, letter := range message {
encryptedLetter, err := modular.Exponentiation(int64(letter), publicExponent, modulus)
if err != nil {
return nil, ErrorFailedToEncrypt
}
encrypted = append(encrypted, rune(encryptedLetter))
}
return encrypted, nil
}
// Decrypt decrypts encrypted rune slice based on the RSA algorithm
func Decrypt(encrypted []rune, privateExponent, modulus int64) (string, error) {
var decrypted []rune
for _, letter := range encrypted {
decryptedLetter, err := modular.Exponentiation(int64(letter), privateExponent, modulus)
if err != nil {
return "", ErrorFailedToDecrypt
}
decrypted = append(decrypted, rune(decryptedLetter))
}
return string(decrypted), nil
}
================================================
FILE: cipher/rsa/rsa2.go
================================================
/*
rsa2.go
description: RSA encryption and decryption including key generation
details: [RSA wiki](https://en.wikipedia.org/wiki/RSA_(cryptosystem))
time complexity: O(n)
space complexity: O(1)
author(s): [ddaniel27](https://github.com/ddaniel27)
*/
package rsa
import (
"encoding/binary"
"fmt"
"math/big"
"math/rand"
"github.com/TheAlgorithms/Go/math/gcd"
"github.com/TheAlgorithms/Go/math/lcm"
"github.com/TheAlgorithms/Go/math/modular"
"github.com/TheAlgorithms/Go/math/prime"
)
// rsa struct contains the public key, private key and modulus
type rsa struct {
publicKey uint64
privateKey uint64
modulus uint64
}
// New initializes the RSA algorithm
// returns the RSA object
func New() *rsa {
// The following code generates keys for RSA encryption/decryption
// 1. Choose two large prime numbers, p and q and compute n = p * q
p, q := randomPrime() // p and q stands for prime numbers
modulus := p * q // n stands for common number
// 2. Compute the totient of n, lcm(p-1, q-1)
totient := uint64(lcm.Lcm(int64(p-1), int64(q-1)))
// 3. Choose an integer e such that 1 < e < totient(n) and gcd(e, totient(n)) = 1
publicKey := uint64(2) // e stands for encryption key (public key)
for publicKey < totient {
if gcd.Recursive(int64(publicKey), int64(totient)) == 1 {
break
}
publicKey++
}
// 4. Compute d such that d * e ≡ 1 (mod totient(n))
inv, _ := modular.Inverse(int64(publicKey), int64(totient))
privateKey := uint64(inv)
return &rsa{
publicKey: publicKey,
privateKey: privateKey,
modulus: modulus,
}
}
// EncryptString encrypts the data using RSA algorithm
// returns the encrypted string
func (rsa *rsa) EncryptString(data string) string {
var nums []byte
for _, char := range data {
slice := make([]byte, 8)
binary.BigEndian.PutUint64( // convert uint64 to byte slice
slice,
encryptDecryptInt(rsa.publicKey, rsa.modulus, uint64(char)), // encrypt each character
)
nums = append(nums, slice...)
}
return string(nums)
}
// DecryptString decrypts the data using RSA algorithm
// returns the decrypted string
func (rsa *rsa) DecryptString(data string) string {
result := ""
middle := []byte(data)
for i := 0; i < len(middle); i += 8 {
if i+8 > len(middle) {
break
}
slice := middle[i : i+8]
num := binary.BigEndian.Uint64(slice) // convert byte slice to uint64
result += fmt.Sprintf("%c", encryptDecryptInt(rsa.privateKey, rsa.modulus, num))
}
return result
}
// GetPublicKey returns the public key and modulus
func (rsa *rsa) GetPublicKey() (uint64, uint64) {
return rsa.publicKey, rsa.modulus
}
// GetPrivateKey returns the private key
func (rsa *rsa) GetPrivateKey() uint64 {
return rsa.privateKey
}
// encryptDecryptInt encrypts or decrypts the data using RSA algorithm
func encryptDecryptInt(e, n, data uint64) uint64 {
pow := new(big.Int).Exp(big.NewInt(int64(data)), big.NewInt(int64(e)), big.NewInt(int64(n)))
return pow.Uint64()
}
// randomPrime returns two random prime numbers
func randomPrime() (uint64, uint64) {
sieve := prime.SieveEratosthenes(1000)
sieve = sieve[10:] // remove first 10 prime numbers (small numbers)
index1 := rand.Intn(len(sieve))
index2 := rand.Intn(len(sieve))
for index1 == index2 {
index2 = rand.Intn(len(sieve))
}
return uint64(sieve[index1]), uint64(sieve[index2])
}
================================================
FILE: cipher/rsa/rsa2_test.go
================================================
package rsa_test
import (
"testing"
"github.com/TheAlgorithms/Go/cipher/rsa"
)
func TestRSA(t *testing.T) {
tests := []struct {
name string
message string
}{
{
name: "Encrypt letter 'a' and decrypt it back",
message: "a",
},
{
name: "Encrypt 'Hello, World!' and decrypt it back",
message: "Hello, World!",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rsa := rsa.New()
encrypted := rsa.EncryptString(tt.message)
decrypted := rsa.DecryptString(encrypted)
if decrypted != tt.message {
t.Errorf("expected %s, got %s", tt.message, decrypted)
}
})
}
}
func BenchmarkRSAEncryption(b *testing.B) {
rsa := rsa.New()
for i := 0; i < b.N; i++ {
rsa.EncryptString("Hello, World!")
}
}
func BenchmarkRSADecryption(b *testing.B) {
rsa := rsa.New()
encrypted := rsa.EncryptString("Hello, World!")
for i := 0; i < b.N; i++ {
rsa.DecryptString(encrypted)
}
}
================================================
FILE: cipher/rsa/rsa_test.go
================================================
// rsa_test.go
// description: Test for RSA Encrypt and Decrypt algorithms
// author(s) [Taj](https://github.com/tjgurwara99)
// see rsa.go
package rsa
import (
"testing"
"github.com/TheAlgorithms/Go/math/gcd"
"github.com/TheAlgorithms/Go/math/lcm"
"github.com/TheAlgorithms/Go/math/modular"
)
var rsaTestData = []struct {
description string
input string
}{
{
"Encrypt letter 'a'",
"a",
},
{
"Encrypt 'hello world'",
"hello world",
},
{
"Encrypt full sentence",
"the quick brown fox jumps over the lazy dog.",
},
{
"Encrypt full sentence from rsacipher.go main function",
"I think RSA is really great",
},
}
func testPrecondition(t *testing.T) (int64, int64, int64) {
// Both prime numbers
t.Helper()
p := int64(61)
q := int64(53)
n := p * q
delta := lcm.Lcm(p-1, q-1)
e := int64(17) // Coprime with delta
if gcd.Recursive(e, delta) != 1 {
t.Fatal("something went wrong: prime numbers are chosen statically and it shouldn't fail at this stage")
}
d, err := modular.Inverse(e, delta)
if err != nil {
t.Fatalf("something went wrong: problem with %q: %v", "modular.Inverse", err)
}
return e, d, n
}
func TestEncryptDecrypt(t *testing.T) {
for _, test := range rsaTestData {
t.Run(test.description, func(t *testing.T) {
e, d, n := testPrecondition(t)
message := []rune(test.input)
encrypted, err := Encrypt(message, e, n)
if err != nil {
t.Fatalf("Failed to Encrypt test string:\n\tDescription: %v\n\tErrMessage: %v", test.description, err)
}
decrypted, err := Decrypt(encrypted, d, n)
if err != nil {
t.Fatalf("Failed to Decrypt test message:\n\tDescription: %v\n\tErrMessage: %v", test.description, err)
}
if actual := test.input; actual != decrypted {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expecting %v, actual %v", decrypted, actual)
}
})
}
}
func FuzzRsa(f *testing.F) {
for _, rsaTestInput := range rsaTestData {
f.Add(rsaTestInput.input)
}
f.Fuzz(func(t *testing.T, input string) {
e, d, n := testPrecondition(t)
encrypted, err := Encrypt([]rune(input), e, n)
if err != nil {
t.Fatalf("failed to encrypt string: %v", err)
}
decrypted, err := Decrypt(encrypted, d, n)
if err != nil {
t.Fatalf("failed to decrypt string: %v", err)
}
if decrypted != input {
t.Fatalf("expected: %q, got: %q", input, decrypted)
}
})
}
================================================
FILE: cipher/transposition/transposition.go
================================================
// transposition.go
// description: Transposition cipher
// details:
// Implementation "Transposition cipher" is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext [Transposition cipher](https://en.wikipedia.org/wiki/Transposition_cipher)
// time complexity: O(n)
// space complexity: O(n)
// author(s) [red_byte](https://github.com/i-redbyte)
// see transposition_test.go
package transposition
import (
"errors"
"fmt"
"sort"
"strings"
)
var ErrNoTextToEncrypt = errors.New("no text to encrypt")
var ErrKeyMissing = errors.New("missing Key")
const placeholder = ' '
func getKey(keyWord string) []int {
keyWord = strings.ToLower(keyWord)
word := []rune(keyWord)
var sortedWord = make([]rune, len(word))
copy(sortedWord, word)
sort.Slice(sortedWord, func(i, j int) bool { return sortedWord[i] < sortedWord[j] })
usedLettersMap := make(map[rune]int)
wordLength := len(word)
resultKey := make([]int, wordLength)
for i := 0; i < wordLength; i++ {
char := word[i]
numberOfUsage := usedLettersMap[char]
resultKey[i] = getIndex(sortedWord, char) + numberOfUsage + 1 //+1 -so that indexing does not start at 0
numberOfUsage++
usedLettersMap[char] = numberOfUsage
}
return resultKey
}
func getIndex(wordSet []rune, subString rune) int {
n := len(wordSet)
for i := 0; i < n; i++ {
if wordSet[i] == subString {
return i
}
}
return 0
}
func Encrypt(text []rune, keyWord string) ([]rune, error) {
key := getKey(keyWord)
keyLength := len(key)
textLength := len(text)
if keyLength <= 0 {
return nil, ErrKeyMissing
}
if textLength <= 0 {
return nil, ErrNoTextToEncrypt
}
if text[len(text)-1] == placeholder {
return nil, fmt.Errorf("%w: cannot encrypt a text, %q, ending with the placeholder char %q", ErrNoTextToEncrypt, text, placeholder)
}
n := textLength % keyLength
for i := 0; i < keyLength-n; i++ {
text = append(text, placeholder)
}
textLength = len(text)
var result []rune
for i := 0; i < textLength; i += keyLength {
transposition := make([]rune, keyLength)
for j := 0; j < keyLength; j++ {
transposition[key[j]-1] = text[i+j]
}
result = append(result, transposition...)
}
return result, nil
}
func Decrypt(text []rune, keyWord string) ([]rune, error) {
key := getKey(keyWord)
textLength := len(text)
if textLength <= 0 {
return nil, ErrNoTextToEncrypt
}
keyLength := len(key)
if keyLength <= 0 {
return nil, ErrKeyMissing
}
n := textLength % keyLength
for i := 0; i < keyLength-n; i++ {
text = append(text, placeholder)
}
var result []rune
for i := 0; i < textLength; i += keyLength {
transposition := make([]rune, keyLength)
for j := 0; j < keyLength; j++ {
transposition[j] = text[i+key[j]-1]
}
result = append(result, transposition...)
}
result = []rune(strings.TrimRight(string(result), string(placeholder)))
return result, nil
}
================================================
FILE: cipher/transposition/transposition_test.go
================================================
// transposition_test.go
// description: Transposition cipher
// author(s) [red_byte](https://github.com/i-redbyte)
// see transposition.go
package transposition
import (
"errors"
"math/rand"
"reflect"
"testing"
)
const enAlphabet = "abcdefghijklmnopqrstuvwxyz"
var texts = []string{
"Ilya Sokolov",
"A slice literal is declared just like an array literal, except you leave out the element count",
"Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.",
"Go’s treatment of errors as values has served us well over the last decade. Although the standard library’s support for errors has been minimal—just the errors.New and fmt.Errorf functions, which produce errors that contain only a message—the built-in error interface allows Go programmers to add whatever information they desire. All it requires is a type that implements an Error method:",
"А тут для примера русский текст",
}
func getRandomString() string {
enRunes := []rune(enAlphabet)
b := make([]rune, rand.Intn(100))
for i := range b {
b[i] = enRunes[rand.Intn(len(enRunes))]
}
return string(b)
}
func TestEncrypt(t *testing.T) {
fn := func(text string, keyWord string) (bool, error) {
encrypt, err := Encrypt([]rune(text), keyWord)
if err != nil && !errors.Is(err, ErrNoTextToEncrypt) && !errors.Is(err, ErrKeyMissing) {
t.Error("Unexpected error ", err)
}
return text == string(encrypt), err
}
for _, s := range texts {
if check, err := fn(s, getRandomString()); check || err != nil {
t.Error("String ", s, " not encrypted")
}
}
if _, err := fn(getRandomString(), ""); err == nil {
t.Error("Error! empty string encryption")
}
}
func TestDecrypt(t *testing.T) {
for _, s := range texts {
keyWord := getRandomString()
encrypt, errEncrypt := Encrypt([]rune(s), keyWord)
if errEncrypt != nil &&
!errors.Is(errEncrypt, ErrNoTextToEncrypt) &&
!errors.Is(errEncrypt, ErrKeyMissing) {
t.Error("Unexpected error ", errEncrypt)
}
if errEncrypt != nil {
t.Error(errEncrypt)
}
decrypt, errDecrypt := Decrypt([]rune(encrypt), keyWord)
if errDecrypt != nil &&
!errors.Is(errDecrypt, ErrNoTextToEncrypt) &&
!errors.Is(errDecrypt, ErrKeyMissing) {
t.Error("Unexpected error ", errDecrypt)
}
if errDecrypt != nil {
t.Error(errDecrypt)
}
if reflect.DeepEqual(encrypt, decrypt) {
t.Error("String ", s, " not encrypted")
}
if reflect.DeepEqual(encrypt, s) {
t.Error("String ", s, " not encrypted")
}
}
}
func TestEncryptDecrypt(t *testing.T) {
text := []rune("Test text for checking the algorithm")
key1 := "testKey"
key2 := "Test Key2"
encrypt, errEncrypt := Encrypt(text, key1)
if errEncrypt != nil {
t.Error(errEncrypt)
}
decrypt, errDecrypt := Decrypt(encrypt, key1)
if errDecrypt != nil {
t.Error(errDecrypt)
}
if !reflect.DeepEqual(decrypt, text) {
t.Errorf("The string was not decrypted correctly %q %q", decrypt, text)
}
decrypt, _ = Decrypt([]rune(encrypt), key2)
if reflect.DeepEqual(decrypt, text) {
t.Errorf("The string was decrypted with a different key: %q %q", decrypt, text)
}
}
func FuzzTransposition(f *testing.F) {
for _, transpositionTestInput := range texts {
f.Add(transpositionTestInput)
}
f.Fuzz(func(t *testing.T, input string) {
keyword := getRandomString()
message := []rune(input)
encrypted, err := Encrypt(message, keyword)
switch {
case err == nil:
case errors.Is(err, ErrKeyMissing),
errors.Is(err, ErrNoTextToEncrypt):
return
default:
t.Fatalf("unexpected error when encrypting string %q: %v", input, err)
}
decrypted, err := Decrypt([]rune(encrypted), keyword)
switch {
case err == nil:
case errors.Is(err, ErrKeyMissing),
errors.Is(err, ErrNoTextToEncrypt):
return
default:
t.Fatalf("unexpected error when decrypting string %q: %v", encrypted, err)
}
if !reflect.DeepEqual(message, decrypted) {
t.Fatalf("expected: %+v, got: %+v", message, []rune(decrypted))
}
})
}
================================================
FILE: cipher/xor/xor.go
================================================
// Package xor is an encryption algorithm that operates the exclusive disjunction(XOR)
// description: XOR encryption
// details: The XOR encryption is an algorithm that operates the exclusive disjunction(XOR) on each character of the plaintext with a given key
// time complexity: O(n)
// space complexity: O(n)
// ref: https://en.wikipedia.org/wiki/XOR_cipher
package xor
// Encrypt encrypts with Xor encryption after converting each character to byte
// The returned value might not be readable because there is no guarantee
// which is within the ASCII range
// If using other type such as string, []int, or some other types,
// add the statements for converting the type to []byte.
func Encrypt(key byte, plaintext []byte) []byte {
cipherText := []byte{}
for _, ch := range plaintext {
cipherText = append(cipherText, key^ch)
}
return cipherText
}
// Decrypt decrypts with Xor encryption
func Decrypt(key byte, cipherText []byte) []byte {
plainText := []byte{}
for _, ch := range cipherText {
plainText = append(plainText, key^ch)
}
return plainText
}
================================================
FILE: cipher/xor/xor_test.go
================================================
package xor
import (
"bytes"
"fmt"
"reflect"
"testing"
)
func Example() {
const (
seed = "Hello World"
key = 97
)
encrypted := Encrypt(byte(key), []byte(seed))
fmt.Printf("Encrypt=> key: %d, seed: %s, encryptedText: %v\n", key, seed, encrypted)
decrypted := Decrypt(byte(key), encrypted)
fmt.Printf("Decrypt=> key: %d, encryptedText: %v, DecryptedText: %s\n", key, encrypted, string(decrypted))
// Output:
// Encrypt=> key: 97, seed: Hello World, encryptedText: [41 4 13 13 14 65 54 14 19 13 5]
// Decrypt=> key: 97, encryptedText: [41 4 13 13 14 65 54 14 19 13 5], DecryptedText: Hello World
}
var xorTestData = []struct {
description string
input string
key int
encrypted string
}{
{
"Encrypt letter 'a' with key 0 makes no changes",
"a",
0,
"a",
},
{
"Encrypt letter 'a' with key 1",
"a",
1,
"`",
},
{
"Encrypt letter 'a' with key 10",
"a",
10,
"k",
},
{
"Encrypt 'hello world' with key 0 makes no changes",
"hello world",
0,
"hello world",
},
{
"Encrypt 'hello world' with key 1",
"hello world",
1,
"idmmn!vnsme",
},
{
"Encrypt 'hello world' with key 10",
"hello world",
10,
"boffe*}exfn",
},
{
"Encrypt full sentence with key 64",
"the quick brown fox jumps over the lazy dog.",
64,
"4(%`15)#+`\"2/7.`&/8`*5-03`/6%2`4(%`,!:9`$/'n",
},
{
"Encrypt a word with key 32 make the case swap",
"abcdefghijklmNOPQRSTUVWXYZ",
32,
"ABCDEFGHIJKLMnopqrstuvwxyz",
},
}
func TestXorCipherEncrypt(t *testing.T) {
for _, test := range xorTestData {
t.Run(test.description, func(t *testing.T) {
encrypted := Encrypt(byte(test.key), []byte(test.input))
if !reflect.DeepEqual(string(encrypted), test.encrypted) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expecting %s, actual %s", test.encrypted, string(encrypted))
}
})
}
}
func TestXorCipherDecrypt(t *testing.T) {
for _, test := range xorTestData {
t.Run(test.description, func(t *testing.T) {
decrypted := Decrypt(byte(test.key), []byte(test.encrypted))
if !reflect.DeepEqual(string(decrypted), test.input) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expecting %s, actual %s", test.input, string(decrypted))
}
})
}
}
func FuzzXOR(f *testing.F) {
f.Add([]byte("The Quick Brown Fox Jumps over the Lazy Dog."), byte('X'))
f.Fuzz(func(t *testing.T, input []byte, key byte) {
cipherText := Encrypt(key, input)
result := Decrypt(key, cipherText)
if !bytes.Equal(input, result) {
t.Errorf("Before: %s, after: %s, key: %d", input, result, key)
}
})
}
================================================
FILE: compression/huffmancoding.go
================================================
// huffman.go
// description: Implements Huffman compression, encoding and decoding
// details:
// We implement the linear-time 2-queue method described here https://en.wikipedia.org/wiki/Huffman_coding.
// It assumes that the list of symbol-frequencies is sorted.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [pedromsrocha](https://github.com/pedromsrocha)
// see also huffmancoding_test.go
package compression
import "fmt"
// A Node of an Huffman tree, which can either be a leaf or an internal node.
// Each node has a weight.
// A leaf node has an associated symbol, but no children (i.e., left == right == nil).
// A parent node has a left and right child and no symbol (i.e., symbol == -1).
type Node struct {
left *Node
right *Node
symbol rune
weight int
}
// A SymbolFreq is a pair of a symbol and its associated frequency.
type SymbolFreq struct {
Symbol rune
Freq int
}
// HuffTree returns the root Node of the Huffman tree by compressing listfreq.
// The compression produces the most optimal code lengths, provided listfreq is ordered,
// i.e.: listfreq[i] <= listfreq[j], whenever i < j.
func HuffTree(listfreq []SymbolFreq) (*Node, error) {
if len(listfreq) < 1 {
return nil, fmt.Errorf("huffman coding: HuffTree : calling method with empty list of symbol-frequency pairs")
}
q1 := make([]Node, len(listfreq))
q2 := make([]Node, 0, len(listfreq))
for i, x := range listfreq { // after the loop, q1 is a slice of leaf nodes representing listfreq
q1[i] = Node{left: nil, right: nil, symbol: x.Symbol, weight: x.Freq}
}
//loop invariant: q1, q2 are ordered by increasing weights
for len(q1)+len(q2) > 1 {
var node1, node2 Node
node1, q1, q2 = least(q1, q2)
node2, q1, q2 = least(q1, q2)
node := Node{left: &node1, right: &node2,
symbol: -1, weight: node1.weight + node2.weight}
q2 = append(q2, node)
}
if len(q1) == 1 { // returns the remaining node in q1, q2
return &q1[0], nil
}
return &q2[0], nil
}
// least removes the node with lowest weight from q1, q2.
// It returns the node with lowest weight and the slices q1, q2 after the update.
func least(q1 []Node, q2 []Node) (Node, []Node, []Node) {
if len(q1) == 0 {
return q2[0], q1, q2[1:]
}
if len(q2) == 0 {
return q1[0], q1[1:], q2
}
if q1[0].weight <= q2[0].weight {
return q1[0], q1[1:], q2
}
return q2[0], q1, q2[1:]
}
// HuffEncoding recursively traverses the Huffman tree pointed by node to obtain
// the map codes, that associates a rune with a slice of booleans.
// Each code is prefixed by prefix and left and right children are labelled with
// the booleans false and true, respectively.
func HuffEncoding(node *Node, prefix []bool, codes map[rune][]bool) {
if node.symbol != -1 { //base case
codes[node.symbol] = prefix
return
}
// inductive step
prefixLeft := make([]bool, len(prefix))
copy(prefixLeft, prefix)
prefixLeft = append(prefixLeft, false)
HuffEncoding(node.left, prefixLeft, codes)
prefixRight := make([]bool, len(prefix))
copy(prefixRight, prefix)
prefixRight = append(prefixRight, true)
HuffEncoding(node.right, prefixRight, codes)
}
// HuffEncode encodes the string in by applying the mapping defined by codes.
func HuffEncode(codes map[rune][]bool, in string) []bool {
out := make([]bool, 0)
for _, s := range in {
out = append(out, codes[s]...)
}
return out
}
// HuffDecode recursively decodes the binary code in, by traversing the Huffman compression tree pointed by root.
// current stores the current node of the traversing algorithm.
// out stores the current decoded string.
func HuffDecode(root, current *Node, in []bool, out string) string {
if current.symbol != -1 {
out += string(current.symbol)
return HuffDecode(root, root, in, out)
}
if len(in) == 0 {
return out
}
if in[0] {
return HuffDecode(root, current.right, in[1:], out)
}
return HuffDecode(root, current.left, in[1:], out)
}
================================================
FILE: compression/huffmancoding_test.go
================================================
// huffmancoding_test.go
// description: Tests the compression, encoding and decoding algorithms of huffmancoding.go.
// author(s) [pedromsrocha](https://github.com/pedromsrocha)
// see huffmancoding.go
package compression_test
import (
"sort"
"testing"
"github.com/TheAlgorithms/Go/compression"
)
// SymbolCountOrd computes sorted symbol-frequency list of input message
func SymbolCountOrd(message string) []compression.SymbolFreq {
runeCount := make(map[rune]int)
for _, s := range message {
runeCount[s]++
}
listfreq := make([]compression.SymbolFreq, len(runeCount))
i := 0
for s, n := range runeCount {
listfreq[i] = compression.SymbolFreq{Symbol: s, Freq: n}
i++
}
sort.Slice(listfreq, func(i, j int) bool { return listfreq[i].Freq < listfreq[j].Freq })
return listfreq
}
func TestHuffman(t *testing.T) {
messages := []string{
"hello world \U0001F600",
"colorless green ideas sleep furiously",
"the quick brown fox jumps over the lazy dog",
`Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.`,
}
for _, message := range messages {
t.Run("huffman: "+message, func(t *testing.T) {
tree, _ := compression.HuffTree(SymbolCountOrd(message))
codes := make(map[rune][]bool)
compression.HuffEncoding(tree, nil, codes)
messageCoded := compression.HuffEncode(codes, message)
messageHuffDecoded := compression.HuffDecode(tree, tree, messageCoded, "")
if messageHuffDecoded != message {
t.Errorf("got: %q\nbut expected: %q", messageHuffDecoded, message)
}
})
}
}
================================================
FILE: compression/rlecoding.go
================================================
/*
rlecoding.go
description: run length encoding and decoding
details:
Run-length encoding (RLE) is a simple form of data compression in which runs of data are stored as a single data value and count, rather than as the original run. This is useful when the data contains many repeated values. For example, the data "WWWWWWWWWWWWBWWWWWWWWWWWWBBB" can be compressed to "12W1B12W3B". The algorithm is simple and can be implemented in a few lines of code.
time complexity: O(n)
space complexity: O(n)
ref: https://en.wikipedia.org/wiki/Run-length_encoding
author(s) [ddaniel27](https://github.com/ddaniel27)
*/
package compression
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
)
// RLEncode takes a string and returns its run-length encoding
func RLEncode(data string) string {
var result string
count := 1
for i := 0; i < len(data); i++ {
if i+1 < len(data) && data[i] == data[i+1] {
count++
continue
}
result += fmt.Sprintf("%d%c", count, data[i])
count = 1
}
return result
}
// RLEdecode takes a run-length encoded string and returns the original string
func RLEdecode(data string) string {
var result string
regex := regexp.MustCompile(`(\d+)(\w)`)
for _, match := range regex.FindAllStringSubmatch(data, -1) {
num, _ := strconv.Atoi(match[1])
result += strings.Repeat(match[2], num)
}
return result
}
// RLEncodebytes takes a byte slice and returns its run-length encoding as a byte slice
func RLEncodebytes(data []byte) []byte {
var result []byte
var count byte = 1
for i := 0; i < len(data); i++ {
if i+1 < len(data) && data[i] == data[i+1] {
count++
continue
}
result = append(result, count, data[i])
count = 1
}
return result
}
// RLEdecodebytes takes a run-length encoded byte slice and returns the original byte slice
func RLEdecodebytes(data []byte) []byte {
var result []byte
for i := 0; i < len(data); i += 2 {
count := int(data[i])
result = append(result, bytes.Repeat([]byte{data[i+1]}, count)...)
}
return result
}
================================================
FILE: compression/rlecoding_test.go
================================================
package compression_test
import (
"bytes"
"testing"
"github.com/TheAlgorithms/Go/compression"
)
func TestCompressionRLEncode(t *testing.T) {
tests := []struct {
name string
data string
want string
}{
{
name: "test 1",
data: "WWWWWWWWWWWWBWWWWWWWWWWWWBBB",
want: "12W1B12W3B",
},
{
name: "test 2",
data: "AABCCCDEEEE",
want: "2A1B3C1D4E",
},
{
name: "test 3",
data: "AAAABBBCCDA",
want: "4A3B2C1D1A",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compression.RLEncode(tt.data); got != tt.want {
t.Errorf("RLEncode() = %v, want %v", got, tt.want)
}
})
}
}
func TestCompressionRLEDecode(t *testing.T) {
tests := []struct {
name string
data string
want string
}{
{
name: "test 1",
data: "12W1B12W3B",
want: "WWWWWWWWWWWWBWWWWWWWWWWWWBBB",
},
{
name: "test 2",
data: "2A1B3C1D4E",
want: "AABCCCDEEEE",
},
{
name: "test 3",
data: "4A3B2C1D1A",
want: "AAAABBBCCDA",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compression.RLEdecode(tt.data); got != tt.want {
t.Errorf("RLEdecode() = %v, want %v", got, tt.want)
}
})
}
}
func TestCompressionRLEncodeBytes(t *testing.T) {
tests := []struct {
name string
data []byte
want []byte
}{
{
name: "test 1",
data: []byte("WWWWWWWWWWWWBWWWWWWWWWWWWBBB"),
want: []byte{12, 'W', 1, 'B', 12, 'W', 3, 'B'},
},
{
name: "test 2",
data: []byte("AABCCCDEEEE"),
want: []byte{2, 'A', 1, 'B', 3, 'C', 1, 'D', 4, 'E'},
},
{
name: "test 3",
data: []byte("AAAABBBCCDA"),
want: []byte{4, 'A', 3, 'B', 2, 'C', 1, 'D', 1, 'A'},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compression.RLEncodebytes(tt.data); !bytes.Equal(got, tt.want) {
t.Errorf("RLEncodebytes() = %v, want %v", got, tt.want)
}
})
}
}
func TestCompressionRLEDecodeBytes(t *testing.T) {
tests := []struct {
name string
data []byte
want []byte
}{
{
name: "test 1",
data: []byte{12, 'W', 1, 'B', 12, 'W', 3, 'B'},
want: []byte("WWWWWWWWWWWWBWWWWWWWWWWWWBBB"),
},
{
name: "test 2",
data: []byte{2, 'A', 1, 'B', 3, 'C', 1, 'D', 4, 'E'},
want: []byte("AABCCCDEEEE"),
},
{
name: "test 3",
data: []byte{4, 'A', 3, 'B', 2, 'C', 1, 'D', 1, 'A'},
want: []byte("AAAABBBCCDA"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compression.RLEdecodebytes(tt.data); !bytes.Equal(got, tt.want) {
t.Errorf("RLEdecodebytes() = %v, want %v", got, tt.want)
}
})
}
}
/* --- BENCHMARKS --- */
func BenchmarkRLEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compression.RLEncode("WWWWWWWWWWWWBWWWWWWWWWWWWBBB")
}
}
func BenchmarkRLEDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compression.RLEdecode("12W1B12W3B")
}
}
func BenchmarkRLEncodeBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compression.RLEncodebytes([]byte("WWWWWWWWWWWWBWWWWWWWWWWWWBBB"))
}
}
func BenchmarkRLEDecodeBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = compression.RLEdecodebytes([]byte{12, 'W', 1, 'B', 12, 'W', 3, 'B'})
}
}
================================================
FILE: constraints/constraints.go
================================================
// Package constraints has some useful generic type constraints defined which is very similar to
// [golang.org/x/exp/constraints](https://pkg.go.dev/golang.org/x/exp/constraints) package.
// We refrained from using that until it gets placed into the standard library - currently
// there are some questions regarding this package [ref](https://github.com/golang/go/issues/50792).
package constraints
// Signed is a generic type constraint for all signed integers.
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
// Unsigned is a generic type constraint for all unsigned integers.
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
// Integer is a generic type constraint for all integers (signed and unsigned.)
type Integer interface {
Signed | Unsigned
}
// Float is a generic type constraint for all floating point types.
type Float interface {
~float32 | ~float64
}
// Number is a generic type constraint for all numeric types in Go except Complex types.
type Number interface {
Integer | Float
}
// Ordered is a generic type constraint for all ordered data types in Go.
// Loosely speaking, in mathematics a field is an ordered field if there is a "total
// order" (a binary relation - in this case `<` symbol) such that we will always have
// if a < b then a + c < b + c and if 0 < a, 0 < b then 0 < a.b
// The idea in Go is quite similar, though only limited to Go standard types
// not user defined types.
type Ordered interface {
Integer | ~string | Float
}
================================================
FILE: conversion/base64.go
================================================
// base64.go
// description: The base64 encoding algorithm as defined in the RFC4648 standard.
// author: [Paul Leydier] (https://github.com/paul-leydier)
// time complexity: O(n)
// space complexity: O(n)
// ref: https://datatracker.ietf.org/doc/html/rfc4648#section-4
// ref: https://en.wikipedia.org/wiki/Base64
// see base64_test.go
package conversion
import (
"strings" // Used for efficient string builder (more efficient than simply appending strings)
)
const Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
// Base64Encode encodes the received input bytes slice into a base64 string.
// The implementation follows the RFC4648 standard, which is documented
// at https://datatracker.ietf.org/doc/html/rfc4648#section-4
func Base64Encode(input []byte) string {
var sb strings.Builder
// If not 24 bits (3 bytes) multiple, pad with 0 value bytes, and with "=" for the output
var padding string
for i := len(input) % 3; i > 0 && i < 3; i++ {
var zeroByte byte
input = append(input, zeroByte)
padding += "="
}
// encode 24 bits per 24 bits (3 bytes per 3 bytes)
for i := 0; i < len(input); i += 3 {
// select 3 8-bit input groups, and re-arrange them into 4 6-bit groups
// the literal 0x3F corresponds to the byte "0011 1111"
// the operation "byte & 0x3F" masks the two left-most bits
group := [4]byte{
input[i] >> 2,
(input[i]<<4)&0x3F + input[i+1]>>4,
(input[i+1]<<2)&0x3F + input[i+2]>>6,
input[i+2] & 0x3F,
}
// translate each group into a char using the static map
for _, b := range group {
sb.WriteString(string(Alphabet[int(b)]))
}
}
encoded := sb.String()
// Apply the output padding
encoded = encoded[:len(encoded)-len(padding)] + padding[:]
return encoded
}
// Base64Decode decodes the received input base64 string into a byte slice.
// The implementation follows the RFC4648 standard, which is documented
// at https://datatracker.ietf.org/doc/html/rfc4648#section-4
func Base64Decode(input string) []byte {
padding := strings.Count(input, "=") // Number of bytes which will be ignored
var decoded []byte
// select 4 6-bit input groups, and re-arrange them into 3 8-bit groups
for i := 0; i < len(input); i += 4 {
// translate each group into a byte using the static map
byteInput := [4]byte{
byte(strings.IndexByte(Alphabet, input[i])),
byte(strings.IndexByte(Alphabet, input[i+1])),
byte(strings.IndexByte(Alphabet, input[i+2])),
byte(strings.IndexByte(Alphabet, input[i+3])),
}
group := [3]byte{
byteInput[0]<<2 + byteInput[1]>>4,
byteInput[1]<<4 + byteInput[2]>>2,
byteInput[2]<<6 + byteInput[3],
}
decoded = append(decoded, group[:]...)
}
return decoded[:len(decoded)-padding]
}
================================================
FILE: conversion/base64_test.go
================================================
package conversion
import "testing"
func TestBase64Encode(t *testing.T) {
testCases := []struct {
in string
expected string
}{
{"Hello World!", "SGVsbG8gV29ybGQh"}, // multiple of 3 byte length (multiple of 24-bits)
{"Hello World!a", "SGVsbG8gV29ybGQhYQ=="}, // multiple of 3 byte length + 1
{"Hello World!ab", "SGVsbG8gV29ybGQhYWI="}, // multiple of 3 byte length + 2
{"", ""}, // empty byte slice
{"6", "Ng=="}, // short text
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg=="}, // Long text
}
for _, tc := range testCases {
result := Base64Encode([]byte(tc.in))
if result != tc.expected {
t.Fatalf("Base64Encode(%s) = %s, want %s", tc.in, result, tc.expected)
}
}
}
func BenchmarkBase64Encode(b *testing.B) {
benchmarks := []struct {
name string
in string
expected string
}{
{"Hello World!", "Hello World!", "SGVsbG8gV29ybGQh"}, // multiple of 3 byte length (multiple of 24-bits)
{"Hello World!a", "Hello World!a", "SGVsbG8gV29ybGQhYQ=="}, // multiple of 3 byte length + 1
{"Hello World!ab", "Hello World!ab", "SGVsbG8gV29ybGQhYWI="}, // multiple of 3 byte length + 2
{"Empty", "", ""}, // empty byte slice
{"6", "6", "Ng=="}, // short text
{"Lorem ipsum", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg=="}, // Long text
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Base64Encode([]byte(bm.in))
}
})
}
}
func TestBase64Decode(t *testing.T) {
testCases := []struct {
expected string
in string
}{
{"Hello World!", "SGVsbG8gV29ybGQh"}, // multiple of 3 byte length (multiple of 24-bits)
{"Hello World!a", "SGVsbG8gV29ybGQhYQ=="}, // multiple of 3 byte length + 1
{"Hello World!ab", "SGVsbG8gV29ybGQhYWI="}, // multiple of 3 byte length + 2
{"", ""}, // empty byte slice
{"6", "Ng=="}, // short text
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg=="}, // Long text
}
for _, tc := range testCases {
result := string(Base64Decode(tc.in))
if result != tc.expected {
t.Fatalf("Base64Decode(%s) = %s, want %s", tc.in, result, tc.expected)
}
}
}
func BenchmarkBase64Decode(b *testing.B) {
benchmarks := []struct {
name string
expected string
in string
}{
{"Hello World!", "Hello World!", "SGVsbG8gV29ybGQh"}, // multiple of 3 byte length (multiple of 24-bits)
{"Hello World!a", "Hello World!a", "SGVsbG8gV29ybGQhYQ=="}, // multiple of 3 byte length + 1
{"Hello World!ab", "Hello World!ab", "SGVsbG8gV29ybGQhYWI="}, // multiple of 3 byte length + 2
{"Empty", "", ""}, // empty byte slice
{"6", "6", "Ng=="}, // short text
{"Lorem ipsum", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg=="}, // Long text
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Base64Decode(bm.in)
}
})
}
}
func TestBase64EncodeDecodeInverse(t *testing.T) {
testCases := []struct {
in string
}{
{"Hello World!"}, // multiple of 3 byte length (multiple of 24-bits)
{"Hello World!a"}, // multiple of 3 byte length + 1
{"Hello World!ab"}, // multiple of 3 byte length + 2
{""}, // empty byte slice
{"6"}, // short text
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}, // Long text
}
for _, tc := range testCases {
result := string(Base64Decode(Base64Encode([]byte(tc.in))))
if result != tc.in {
t.Fatalf("Base64Decode(Base64Encode(%s)) = %s, want %s", tc.in, result, tc.in)
}
}
}
func FuzzBase64Encode(f *testing.F) {
f.Add([]byte("hello"))
f.Fuzz(func(t *testing.T, input []byte) {
result := Base64Decode(Base64Encode(input))
for i := 0; i < len(input); i++ {
if result[i] != input[i] {
t.Fatalf("with input '%s' - expected '%s', got '%s' (mismatch at position %d)", input, input, result, i)
}
}
})
}
================================================
FILE: conversion/binarytodecimal.go
================================================
/*
Author: Motasim
GitHub: https://github.com/motasimmakki
Date: 19-Oct-2021
*/
// This algorithm will convert any Binary number(0 or 1) to Decimal number(+ve number).
// https://en.wikipedia.org/wiki/Binary_number
// https://en.wikipedia.org/wiki/Decimal
// Function receives a Binary Number as string and returns the Decimal number as integer.
// Supported Binary number range is 0 to 2^(31-1).
// time complexity: O(n)
// space complexity: O(1)
package conversion
// Importing necessary package.
import (
"errors"
"regexp"
)
var isValid = regexp.MustCompile("^[0-1]{1,}$").MatchString
// BinaryToDecimal() function that will take Binary number as string,
// and return its Decimal equivalent as an integer.
func BinaryToDecimal(binary string) (int, error) {
if !isValid(binary) {
return -1, errors.New("not a valid binary string")
}
if len(binary) > 32 {
return -1, errors.New("binary number must be in range 0 to 2^(31-1)")
}
var result, base int = 0, 1
for i := len(binary) - 1; i >= 0; i-- {
if binary[i] == '1' {
result += base
}
base *= 2
}
return result, nil
}
================================================
FILE: conversion/binarytodecimal_test.go
================================================
package conversion
import "testing"
var binaryTestCases = map[string]int{
"0": 0, "1": 1, "10": 2, "11": 3, "100": 4,
"101": 5, "110": 6, "111": 7, "1000": 8, "1001": 9,
"1010": 10, "1011": 11, "1100": 12, "1101": 13, "1110": 14,
"1111": 15, "10000": 16, "10001": 17, "10010": 18, "10011": 19,
"10100": 20, "10101": 21, "10110": 22, "10111": 23, "11000": 24,
"11001": 25, "11010": 26, "11011": 27, "11100": 28, "11101": 29,
"11110": 30, "11111": 31, "100000": 32, "100001": 33, "100010": 34,
"100011": 35, "100100": 36, "100101": 37, "100110": 38, "100111": 39,
"101000": 40, "101001": 41, "101010": 42, "101011": 43, "101100": 44,
"101101": 45, "101110": 46, "101111": 47, "110000": 48, "110001": 49,
"110010": 50, "110011": 51, "110100": 52, "110101": 53, "110110": 54,
"110111": 55, "111000": 56, "111001": 57, "111010": 58, "111011": 59,
"111100": 60, "111101": 61, "111110": 62, "111111": 63, "1000000": 64,
"1000001": 65, "1000010": 66, "1000011": 67, "1000100": 68, "1000101": 69,
"1000110": 70, "1000111": 71, "1001000": 72, "1001001": 73, "1001010": 74,
"1001011": 75, "1001100": 76, "1001101": 77, "1001110": 78, "1001111": 79,
"1010000": 80, "1010001": 81, "1010010": 82, "1010011": 83, "1010100": 84,
"1010101": 85, "1010110": 86, "1010111": 87, "1011000": 88, "1011001": 89,
"1011010": 90, "1011011": 91, "1011100": 92, "1011101": 93, "1011110": 94,
"1011111": 95, "1100000": 96, "1100001": 97, "1100010": 98, "1100011": 99,
"1100100": 100,
}
func TestBinaryToDecimal(t *testing.T) {
for input, expected := range binaryTestCases {
out, err := BinaryToDecimal(input)
if err != nil {
t.Errorf("BinaryToDecimal(%s) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("BinaryToDecimal(%s) = %d; want %d", input, out, expected)
}
}
}
func BenchmarkBinaryToDecimal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = BinaryToDecimal("1100100")
}
}
================================================
FILE: conversion/conversion_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package conversion
================================================
FILE: conversion/decimaltobinary.go
================================================
/*
Author: Motasim
GitHub: https://github.com/motasimmakki
Date: 14-Oct-2021
*/
// This algorithm will convert any Decimal (+ve integer) number to Binary number.
// https://en.wikipedia.org/wiki/Binary_number
// Function receives a integer as a Decimal number and returns the Binary number.
// Supported integer value range is 0 to 2^(31 -1).
// time complexity: O(log(n))
// space complexity: O(1)
package conversion
// Importing necessary package.
import (
"errors"
"strconv"
)
// Reverse() function that will take string,
// and returns the reverse of that string.
func Reverse(str string) string {
rStr := []rune(str)
for i, j := 0, len(rStr)-1; i < len(rStr)/2; i, j = i+1, j-1 {
rStr[i], rStr[j] = rStr[j], rStr[i]
}
return string(rStr)
}
// DecimalToBinary() function that will take Decimal number as int,
// and return its Binary equivalent as a string.
func DecimalToBinary(num int) (string, error) {
if num < 0 {
return "", errors.New("integer must have +ve value")
}
if num == 0 {
return "0", nil
}
var result string = ""
for num > 0 {
result += strconv.Itoa(num & 1)
num >>= 1
}
return Reverse(result), nil
}
================================================
FILE: conversion/decimaltobinary_test.go
================================================
package conversion
import "testing"
var decimalTestCases = map[int]string{
0: "0", 1: "1", 2: "10", 3: "11", 4: "100",
5: "101", 6: "110", 7: "111", 8: "1000", 9: "1001",
10: "1010", 11: "1011", 12: "1100", 13: "1101", 14: "1110",
15: "1111", 16: "10000", 17: "10001", 18: "10010", 19: "10011",
20: "10100", 21: "10101", 22: "10110", 23: "10111", 24: "11000",
25: "11001", 26: "11010", 27: "11011", 28: "11100", 29: "11101",
30: "11110", 31: "11111", 32: "100000", 33: "100001", 34: "100010",
35: "100011", 36: "100100", 37: "100101", 38: "100110", 39: "100111",
40: "101000", 41: "101001", 42: "101010", 43: "101011", 44: "101100",
45: "101101", 46: "101110", 47: "101111", 48: "110000", 49: "110001",
50: "110010", 51: "110011", 52: "110100", 53: "110101", 54: "110110",
55: "110111", 56: "111000", 57: "111001", 58: "111010", 59: "111011",
60: "111100", 61: "111101", 62: "111110", 63: "111111", 64: "1000000",
65: "1000001", 66: "1000010", 67: "1000011", 68: "1000100", 69: "1000101",
70: "1000110", 71: "1000111", 72: "1001000", 73: "1001001", 74: "1001010",
75: "1001011", 76: "1001100", 77: "1001101", 78: "1001110", 79: "1001111",
80: "1010000", 81: "1010001", 82: "1010010", 83: "1010011", 84: "1010100",
85: "1010101", 86: "1010110", 87: "1010111", 88: "1011000", 89: "1011001",
90: "1011010", 91: "1011011", 92: "1011100", 93: "1011101", 94: "1011110",
95: "1011111", 96: "1100000", 97: "1100001", 98: "1100010", 99: "1100011",
100: "1100100",
}
func TestDecimalToBinary(t *testing.T) {
for input, expected := range decimalTestCases {
out, err := DecimalToBinary(input)
if err != nil {
t.Errorf("DecimalToBinary(%d) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("DecimalToBinary(%d) = %s; want %s", input, out, expected)
}
}
}
func BenchmarkDecimalToBinary(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = DecimalToBinary(100)
}
}
================================================
FILE: conversion/doc.go
================================================
// Package conversion is a package of implementations which converts one data structure to another.
package conversion
================================================
FILE: conversion/hexadecimaltobinary.go
================================================
/*
Author: mapcrafter2048
GitHub: https://github.com/mapcrafter2048
*/
// This algorithm will convert any Hexadecimal number(0-9, A-F, a-f) to Binary number(0 or 1).
// https://en.wikipedia.org/wiki/Hexadecimal
// https://en.wikipedia.org/wiki/Binary_number
// Function receives a Hexadecimal Number as string and returns the Binary number as string.
// Supported Hexadecimal number range is 0 to 7FFFFFFFFFFFFFFF.
package conversion
import (
"errors"
"regexp"
"strings"
)
var isValidHex = regexp.MustCompile("^[0-9A-Fa-f]+$").MatchString
// hexToBinary() function that will take Hexadecimal number as string,
// and return its Binary equivalent as a string.
func hexToBinary(hex string) (string, error) {
// Trim any leading or trailing whitespace
hex = strings.TrimSpace(hex)
// Check if the hexadecimal string is empty
if hex == "" {
return "", errors.New("input string is empty")
}
// Check if the hexadecimal string is valid
if !isValidHex(hex) {
return "", errors.New("invalid hexadecimal string: " + hex)
}
// Parse the hexadecimal string to an integer
var decimal int64
for i := 0; i < len(hex); i++ {
char := hex[i]
var value int64
if char >= '0' && char <= '9' {
value = int64(char - '0')
} else if char >= 'A' && char <= 'F' {
value = int64(char - 'A' + 10)
} else if char >= 'a' && char <= 'f' {
value = int64(char - 'a' + 10)
} else {
return "", errors.New("invalid character in hexadecimal string: " + string(char))
}
decimal = decimal*16 + value
}
// Convert the integer to a binary string without using predefined functions
var binaryBuilder strings.Builder
if decimal == 0 {
binaryBuilder.WriteString("0")
} else {
for decimal > 0 {
bit := decimal % 2
if bit == 0 {
binaryBuilder.WriteString("0")
} else {
binaryBuilder.WriteString("1")
}
decimal = decimal / 2
}
}
// Reverse the binary string since the bits are added in reverse order
binaryRunes := []rune(binaryBuilder.String())
for i, j := 0, len(binaryRunes)-1; i < j; i, j = i+1, j-1 {
binaryRunes[i], binaryRunes[j] = binaryRunes[j], binaryRunes[i]
}
return string(binaryRunes), nil
}
================================================
FILE: conversion/hexadecimaltobinary_test.go
================================================
package conversion
import (
"testing"
)
func TestHexToBinary(t *testing.T) {
tests := []struct {
hex string
want string
wantErr bool
}{
{"", "", true},
{"G123", "", true},
{"12XZ", "", true},
{"1", "1", false},
{"A", "1010", false},
{"10", "10000", false},
{"1A", "11010", false},
{"aB", "10101011", false},
{"0Ff", "11111111", false},
{" 1A ", "11010", false},
{"0001A", "11010", false},
{"7FFFFFFFFFFFFFFF", "111111111111111111111111111111111111111111111111111111111111111", false},
}
for _, tt := range tests {
t.Run(tt.hex, func(t *testing.T) {
got, err := hexToBinary(tt.hex)
if (err != nil) != tt.wantErr {
t.Errorf("hexToBinary(%q) error = %v, wantErr %v", tt.hex, err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("hexToBinary(%q) = %v, want %v", tt.hex, got, tt.want)
}
})
}
}
func BenchmarkHexToBinary(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = hexToBinary("7FFFFFFFFFFFFFFF")
}
}
================================================
FILE: conversion/hexadecimaltodecimal.go
================================================
/*
Author: mapcrafter2048
GitHub: https://github.com/mapcrafter2048
*/
// This algorithm will convert any Hexadecimal number(0-9, A-F, a-f) to Decimal number(0-9).
// https://en.wikipedia.org/wiki/Hexadecimal
// https://en.wikipedia.org/wiki/Decimal
// Function receives a Hexadecimal Number as string and returns the Decimal number as integer.
// Supported Hexadecimal number range is 0 to 7FFFFFFFFFFFFFFF.
package conversion
import (
"fmt"
"regexp"
"strings"
)
var isValidHexadecimal = regexp.MustCompile("^[0-9A-Fa-f]+$").MatchString
// hexToDecimal converts a hexadecimal string to a decimal integer.
func hexToDecimal(hexStr string) (int64, error) {
hexStr = strings.TrimSpace(hexStr)
if len(hexStr) == 0 {
return 0, fmt.Errorf("input string is empty")
}
// Check if the string has a valid hexadecimal prefix
if len(hexStr) > 2 && (hexStr[:2] == "0x" || hexStr[:2] == "0X") {
hexStr = hexStr[2:]
}
// Validate the hexadecimal string
if !isValidHexadecimal(hexStr) {
return 0, fmt.Errorf("invalid hexadecimal string")
}
var decimalValue int64
for _, char := range hexStr {
var digit int64
if char >= '0' && char <= '9' {
digit = int64(char - '0')
} else if char >= 'A' && char <= 'F' {
digit = int64(char - 'A' + 10)
} else if char >= 'a' && char <= 'f' {
digit = int64(char - 'a' + 10)
} else {
return 0, fmt.Errorf("invalid character in hexadecimal string: %c", char)
}
decimalValue = decimalValue*16 + digit
}
return decimalValue, nil
}
================================================
FILE: conversion/hexadecimaltodecimal_test.go
================================================
package conversion
import (
"testing"
)
func TestHexToDecimal(t *testing.T) {
tests := []struct {
hex string
want int64
wantErr bool
}{
{"", 0, true},
{"G123", 0, true},
{"123Z", 0, true},
{"1", 1, false},
{"A", 10, false},
{"10", 16, false},
{"1A", 26, false},
{"aB", 171, false},
{"0Ff", 255, false},
{" 1A ", 26, false},
{"0x1A", 26, false},
{"0X1A", 26, false},
{"1A", 26, false},
{"7FFFFFFFFFFFFFFF", 9223372036854775807, false},
{"0001A", 26, false},
{"0000007F", 127, false},
{"0", 0, false},
{"0x0", 0, false},
}
for _, tt := range tests {
t.Run(tt.hex, func(t *testing.T) {
got, err := hexToDecimal(tt.hex)
if (err != nil) != tt.wantErr {
t.Errorf("hexToDecimal(%q) error = %v, wantErr %v", tt.hex, err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("hexToDecimal(%q) = %v, want %v", tt.hex, got, tt.want)
}
})
}
}
func BenchmarkHexToDecimal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = hexToDecimal("7FFFFFFFFFFFFFFF")
}
}
================================================
FILE: conversion/inttoroman.go
================================================
// inttoroman.go
// description: Convert an integer to a roman numeral
// details: This program converts an integer to a roman numeral. The program uses a lookup array to convert the integer to a roman numeral.
// time complexity: O(1)
// space complexity: O(1)
package conversion
import (
"errors"
)
var (
// lookup arrays used for converting from an int to a roman numeral extremely quickly.
r0 = []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"} // 1 - 9
r1 = []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"} // 10 - 90
r2 = []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"} // 100 - 900
r3 = []string{"", "M", "MM", "MMM"} // 1,000 - 3,000
)
// IntToRoman converts an integer value to a roman numeral string. An error is
// returned if the integer is not between 1 and 3999.
func IntToRoman(n int) (string, error) {
if n < 1 || n > 3999 {
return "", errors.New("integer must be between 1 and 3999")
}
// Concatenate strings for each of 4 lookup array categories.
//
// Key behavior to note here is how math with integers is handled. Values are floored to the
// nearest int, not rounded up. For example, 26/10 = 2 even though the actual result is 2.6.
//
// For example, lets use an input value of 126:
// `r3[n%1e4/1e3]` --> 126 % 10_000 = 126 --> 126 / 1_000 = 0.126 (0 as int) --> r3[0] = ""
// `r2[n%1e3/1e2]` --> 126 % 1_000 = 126 --> 126 / 100 = 1.26 (1 as int) --> r2[1] = "C"
// `r1[n%100/10]` --> 126 % 100 = 26 --> 26 / 10 = 2.6 (2 as int) --> r1[2] = "XX"
// `r0[n%10]` --> 126 % 10 = 6 --> r0[6] = "VI"
// FINAL --> "" + "C" + "XX" + "VI" = "CXXVI"
//
// This is efficient in Go. The 4 operands are evaluated,
// then a single allocation is made of the exact size needed for the result.
return r3[n%1e4/1e3] + r2[n%1e3/1e2] + r1[n%100/10] + r0[n%10], nil
}
================================================
FILE: conversion/inttoroman_test.go
================================================
package conversion
import "testing"
func TestIntToRoman(t *testing.T) {
for expected, input := range romanTestCases {
out, err := IntToRoman(input)
if err != nil {
t.Errorf("IntToRoman(%d) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("IntToRoman(%d) = %s; want %s", input, out, expected)
}
}
_, err := IntToRoman(100000)
if err == nil {
t.Errorf("IntToRoman(%d) expected an error", 100000)
}
_, err = IntToRoman(0)
if err == nil {
t.Errorf("IntToRoman(%d) expected an error", 0)
}
}
func BenchmarkIntToRoman(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = IntToRoman(3999)
}
}
================================================
FILE: conversion/rgbhex.go
================================================
// rgbhex.go
// description: convert hex input to red, green and blue and vice versa
// time complexity: O(1)
// space complexity: O(1)
// author(s) [darmiel](https://github.com/darmiel)
// see rgbhex_test.go
package conversion
// HEXToRGB splits an RGB input (e.g. a color in hex format; 0x)
// into the individual components: red, green and blue
func HEXToRGB(hex uint) (red, green, blue byte) {
// A hex code is structured like this:
// #3498db (light blue) - converted to binary:
// 00110100 10011000 11011011
//
// To get the blue value we use the bit operation AND with the bit mask 0xFF (in binary: 11111111)
// 00110100 10011000 <11011011> &
// 00000000 00000000 11111111 =
// 00000000 00000000 <11011011> =
blue = byte(hex & 0xFF)
// To get the green value, we first shift the value 8 bits to the right:
// 00110100 <10011000> 11011011 >> 8 =
// 00000000 00110100 <10011000> &
// 00000000 00000000 11111111 =
// 00000000 00000000 <10011000> =
green = byte((hex >> 8) & 0xFF)
// Same as green value, only this time shift 16 to the right
// Alternatively, you can apply a bitmask first and then shift it.
// <00110100> 10011000 11011011 &
// 11111111 00000000 00000000 =
// <00110100> 00000000 00000000 >> 16
// 00000000 00000000 <00110100> =
red = byte((hex >> 16) & 0xFF)
return
}
// RGBToHEX does exactly the opposite of HEXToRGB:
// it combines the three components red, green and blue to an RGB value, which can be converted to e.g. Hex
func RGBToHEX(red, green, blue byte) (hex uint) {
// Sets the bits of blue in position 1-8, green in 9-16 and red in 17-24
// Red: 00110100
// Green: 10011000
// Blue: 11011011
// RGB:
// R << 16: [00110100] 00000000 00000000 |
// G << 8 : 00000000 {10011000} 00000000 |
// B : 00000000 00000000 <11011011> =
// [00110100] {10011000} <11011011>
return (uint(red) << 16) | (uint(green) << 8) | uint(blue)
}
================================================
FILE: conversion/rgbhex_test.go
================================================
package conversion
import "testing"
var HEX = []uint{
0x1abc9c,
0x3498db,
0x9b59b6,
}
var RGB = [][]byte{
{26, 188, 156},
{52, 152, 219},
{155, 89, 182},
}
func TestHEXToRGB(t *testing.T) {
for i := 0; i < len(HEX); i++ {
hex := HEX[i]
expected := RGB[i]
resultR, resultG, resultB := HEXToRGB(hex)
if resultR != expected[0] || resultG != expected[1] || resultB != expected[2] {
t.Errorf("HEXToRGB(%d) = %d,%d,%d; want %d,%d,%d",
hex, resultR, resultG, resultB, expected[0], expected[1], expected[2])
}
}
}
func BenchmarkHEXToRGB(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _ = HEXToRGB(0xdeadbe)
}
}
func TestRGBToHEX(t *testing.T) {
for i := 0; i < len(RGB); i++ {
args := RGB[i]
expected := HEX[i]
result := RGBToHEX(args[0], args[1], args[2])
if result != expected {
t.Errorf("RGBToHEX(%d,%d,%d) = %d; want %d",
args[0], args[1], args[2], result, expected)
}
}
}
func BenchmarkRGBToHEX(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = RGBToHEX(222, 173, 190)
}
}
================================================
FILE: conversion/romantoint.go
================================================
// This algorithm will convert a standard roman number to an integer
// https://en.wikipedia.org/wiki/Roman_numerals
// Function receives a string as a roman number and outputs an integer
// Maximum output will be 3999
// Only standard form is supported
// time complexity: O(n)
// space complexity: O(1)
package conversion
import (
"errors"
"strings"
)
// numeral describes the value and symbol of a single roman numeral
type numeral struct {
val int
sym string
}
// lookup array for numeral values sorted by largest to smallest
var nums = []numeral{
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"},
}
// RomanToInt converts a roman numeral string to an integer. Roman numerals for numbers
// outside the range 1 to 3,999 will return an error. Nil or empty string return 0
// with no error thrown.
func RomanToInt(input string) (int, error) {
if input == "" {
return 0, nil
}
var output int
for _, n := range nums {
for strings.HasPrefix(input, n.sym) {
output += n.val
input = input[len(n.sym):]
}
}
// if we are still left with input string values then the
// input was invalid and an error is returned.
if len(input) > 0 {
return 0, errors.New("invalid roman numeral")
}
return output, nil
}
================================================
FILE: conversion/romantoint_test.go
================================================
package conversion
import "testing"
var romanTestCases = map[string]int{
"I": 1, "II": 2, "III": 3, "IV": 4, "V": 5, "VI": 6,
"VII": 7, "VIII": 8, "IX": 9, "X": 10, "XI": 11, "XII": 12,
"XIII": 13, "XIV": 14, "XV": 15, "XVI": 16, "XVII": 17,
"XVIII": 18, "XIX": 19, "XX": 20, "XXXI": 31, "XXXII": 32,
"XXXIII": 33, "XXXIV": 34, "XXXV": 35, "XXXVI": 36, "XXXVII": 37,
"XXXVIII": 38, "XXXIX": 39, "XL": 40, "XLI": 41, "XLII": 42,
"XLIII": 43, "XLIV": 44, "XLV": 45, "XLVI": 46, "XLVII": 47,
"XLVIII": 48, "XLIX": 49, "L": 50, "LXXXIX": 89, "XC": 90,
"XCI": 91, "XCII": 92, "XCIII": 93, "XCIV": 94, "XCV": 95,
"XCVI": 96, "XCVII": 97, "XCVIII": 98, "XCIX": 99, "C": 100,
"CI": 101, "CII": 102, "CIII": 103, "CIV": 104, "CV": 105,
"CVI": 106, "CVII": 107, "CVIII": 108, "CIX": 109, "CXLIX": 149,
"CCCXLIX": 349, "CDLVI": 456, "D": 500, "DCIV": 604, "DCCLXXXIX": 789,
"DCCCXLIX": 849, "CMIV": 904, "M": 1000, "MVII": 1007, "MLXVI": 1066,
"MCCXXXIV": 1234, "MDCCLXXVI": 1776, "MMXXI": 2021, "MMDCCCVI": 2806,
"MMCMXCIX": 2999, "MMM": 3000, "MMMCMLXXIX": 3979, "MMMCMXCIX": 3999,
}
func TestRomanToInt(t *testing.T) {
for input, expected := range romanTestCases {
out, err := RomanToInt(input)
if err != nil {
t.Errorf("RomanToInt(%s) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("RomanToInt(%s) = %d; want %d", input, out, expected)
}
}
_, err := RomanToInt("IVCMXCIX")
if err == nil {
t.Error("RomanToInt(IVCMXCIX) expected an error")
}
val, err := RomanToInt("")
if val != 0 {
t.Errorf("RomanToInt(\"\") = %d; want 0", val)
}
if err != nil {
t.Errorf("RomanToInt(\"\") returned an error %s", err.Error())
}
}
func BenchmarkRomanToInt(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = RomanToInt("MMMCMXCIX")
}
}
================================================
FILE: dynamic/abbreviation.go
================================================
// File: abbreviation.go
// Description: Abbreviation problem
// Details:
// https://www.hackerrank.com/challenges/abbr/problem
// Problem description (from hackerrank):
// You can perform the following operations on the string, a:
// 1. Capitalize zero or more of a's lowercase letters.
// 2. Delete all of the remaining lowercase letters in a.
// Given 2 strings a and b, determine if it's possible to make a equal to be using above operations.
// Example:
// Given a = "ABcde" and b = "ABCD"
// We can capitalize "c" and "d" in a to get "ABCde" then delete all the lowercase letters (which is only "e") in a to get "ABCD" which equals b.
// Author: [duongoku](https://github.com/duongoku)
// Time Complexity: O(n*m) where n is the length of a and m is the length of b
// Space Complexity: O(n*m) where n is the length of a and m is the length of b
// See abbreviation_test.go for test cases
package dynamic
// strings for getting uppercases and lowercases
import (
"strings"
)
// Returns true if it is possible to make a equals b (if b is an abbreviation of a), returns false otherwise
func Abbreviation(a string, b string) bool {
dp := make([][]bool, len(a)+1)
for i := range dp {
dp[i] = make([]bool, len(b)+1)
}
dp[0][0] = true
for i := 0; i < len(a); i++ {
for j := 0; j <= len(b); j++ {
if dp[i][j] {
if j < len(b) && strings.ToUpper(string(a[i])) == string(b[j]) {
dp[i+1][j+1] = true
}
if string(a[i]) == strings.ToLower(string(a[i])) {
dp[i+1][j] = true
}
}
}
}
return dp[len(a)][len(b)]
}
================================================
FILE: dynamic/abbreviation_test.go
================================================
package dynamic
import (
"fmt"
"testing"
)
func TestAbbreviation(t *testing.T) {
tests := []struct {
a string
b string
expected bool
}{
{"uOHlGMdUBc", "uOalGMdUBCasdcavsdf", false},
{"kotgDIUagj", "DIU", true},
{"WPTffVkSNl", "WPTVSN", true},
{"CoJsPURrVX", "CPUVX", false},
{"xasreDHndqvCnFfX", "DHndqvCnFX", false},
{"XFEaWCxpeepGjOnCCsFh", "XFEAWCPEPGOCCSF", true},
{"", "", true},
{"a", "", true},
{"a", "b", false},
{"a", "a", false},
{"A", "A", true},
}
count := len(tests)
for i := 0; i < count; i++ {
name := fmt.Sprintf(
"Test case #%d: string a = \"%s\", string b = \"%s\"",
i+1,
tests[i].a,
tests[i].b,
)
t.Run(name, func(t *testing.T) {
result := Abbreviation(tests[i].a, tests[i].b)
if result != tests[i].expected {
t.Errorf("Expected the %t, got %t", tests[i].expected, result)
}
})
}
}
================================================
FILE: dynamic/binomialcoefficient.go
================================================
// binomialcoefficient.go
// description: Implementation of the binomial coefficient using dynamic programming
// details: The binomial coefficient C(n, k) is the number of ways to choose a subset of k elements from a set of n elements. The binomial coefficient is calculated using the formula C(n, k) = C(n-1, k-1) + C(n-1, k) with base cases C(n, 0) = C(n, n) = 1.
// time complexity: O(n*k) where n is the number of elements and k is the number of elements to choose
// space complexity: O(n*k) where n is the number of elements and k is the number of elements to choose
package dynamic
import "github.com/TheAlgorithms/Go/math/min"
// func main() {
// myArrayOfK := [4]int{5, 6, 7, 8}
// var x int
// fmt.Println("\nBinomial Coefficient Using Dynamic Programming:", bin2(50, 5))
// for _, element := range myArrayOfK {
// start := time.Now()
// x = bin2(50, element)
// elapsed := time.Since(start)
// fmt.Println("bin2 (50,", element, ") = ", x, " took ", elapsed)
// }
// }
// Bin2 function
func Bin2(n int, k int) int {
var i, j int
B := make([][]int, (n + 1))
for i := range B {
B[i] = make([]int, k+1)
}
for i = 0; i <= n; i++ {
for j = 0; j <= min.Int(i, k); j++ {
if j == 0 || j == i {
B[i][j] = 1
} else {
B[i][j] = B[i-1][j-1] + B[i-1][j]
}
}
}
return B[n][k]
}
================================================
FILE: dynamic/binomialcoefficient_test.go
================================================
package dynamic_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
func TestBin2(t *testing.T) {
td := []struct {
n, k, expected int
}{
{0, 0, 1},
{1, 1, 1},
{2, 0, 1}, {2, 1, 2}, {2, 2, 1},
{3, 0, 1}, {3, 1, 3}, {3, 2, 3}, {3, 3, 1},
{4, 0, 1}, {4, 1, 4}, {4, 2, 6}, {4, 3, 4}, {4, 4, 1},
{5, 0, 1}, {5, 1, 5}, {5, 2, 10}, {5, 3, 10}, {5, 4, 5}, {5, 5, 1},
{10, 2, 45},
{15, 10, 3003},
}
for _, tc := range td {
name := fmt.Sprintf("binomial coefficient of (%d, %d)", tc.n, tc.k)
t.Run(name, func(t *testing.T) {
actual := dynamic.Bin2(tc.n, tc.k)
if actual != tc.expected {
t.Errorf("expecting binomial coefficient of (%d, %d) to be %d but got %d", tc.n, tc.k, tc.expected, actual)
}
})
}
}
================================================
FILE: dynamic/burstballoons.go
================================================
package dynamic
import "github.com/TheAlgorithms/Go/math/max"
// MaxCoins returns the maximum coins we can collect by bursting the balloons
func MaxCoins(nums []int) int {
n := len(nums)
if n == 0 {
return 0
}
nums = append([]int{1}, nums...)
nums = append(nums, 1)
dp := make([][]int, n+2)
for i := range dp {
dp[i] = make([]int, n+2)
}
for length := 1; length <= n; length++ {
for left := 1; left+length-1 <= n; left++ {
right := left + length - 1
for k := left; k <= right; k++ {
coins := nums[left-1] * nums[k] * nums[right+1]
dp[left][right] = max.Int(dp[left][right], dp[left][k-1]+dp[k+1][right]+coins)
}
}
}
return dp[1][n]
}
================================================
FILE: dynamic/burstballoons_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseBurstBalloons struct {
nums []int
expected int
}
func getBurstBalloonsTestCases() []testCaseBurstBalloons {
return []testCaseBurstBalloons{
{[]int{3, 1, 5, 8}, 167}, // Maximum coins from [3,1,5,8]
{[]int{1, 5}, 10}, // Maximum coins from [1,5]
{[]int{1}, 1}, // Single balloon
{[]int{}, 0}, // No balloons
}
}
func TestMaxCoins(t *testing.T) {
t.Run("Burst Balloons test cases", func(t *testing.T) {
for _, tc := range getBurstBalloonsTestCases() {
actual := dynamic.MaxCoins(tc.nums)
if actual != tc.expected {
t.Errorf("MaxCoins(%v) = %d; expected %d", tc.nums, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/catalan.go
================================================
//The Catalan numbers are a sequence of positive integers that appear in many counting
// problems in combinatorics.
// time complexity: O(n²)
// space complexity: O(n)
//reference: https://brilliant.org/wiki/catalan-numbers/
package dynamic
import "fmt"
var errCatalan = fmt.Errorf("can't have a negative n-th catalan number")
// NthCatalan returns the n-th Catalan Number
// Complexity: O(n²)
func NthCatalanNumber(n int) (int64, error) {
if n < 0 {
//doesn't accept negative number
return 0, errCatalan
}
var catalanNumberList []int64
catalanNumberList = append(catalanNumberList, 1) //first value is 1
for i := 1; i <= n; i++ {
catalanNumberList = append(catalanNumberList, 0) //append 0 and calculate
for j := 0; j < i; j++ {
catalanNumberList[i] += catalanNumberList[j] * catalanNumberList[i-j-1]
}
}
return catalanNumberList[n], nil
}
================================================
FILE: dynamic/catalan_test.go
================================================
package dynamic
import (
"fmt"
"testing"
)
func TestCatalanNumbers(t *testing.T) {
var testCatalanNumbersData = []struct {
nthCatalanNumber int
expectedCatalanNumber int64
expectedError error
}{
{nthCatalanNumber: -1000, expectedCatalanNumber: 0, expectedError: errCatalan},
{nthCatalanNumber: -1, expectedCatalanNumber: 0, expectedError: errCatalan},
{nthCatalanNumber: 0, expectedCatalanNumber: 1, expectedError: nil},
{nthCatalanNumber: 1, expectedCatalanNumber: 1, expectedError: nil},
{nthCatalanNumber: 2, expectedCatalanNumber: 2, expectedError: nil},
{nthCatalanNumber: 3, expectedCatalanNumber: 5, expectedError: nil},
{nthCatalanNumber: 4, expectedCatalanNumber: 14, expectedError: nil},
{nthCatalanNumber: 5, expectedCatalanNumber: 42, expectedError: nil},
{nthCatalanNumber: 6, expectedCatalanNumber: 132, expectedError: nil},
{nthCatalanNumber: 7, expectedCatalanNumber: 429, expectedError: nil},
{nthCatalanNumber: 8, expectedCatalanNumber: 1430, expectedError: nil},
{nthCatalanNumber: 9, expectedCatalanNumber: 4862, expectedError: nil},
{nthCatalanNumber: 10, expectedCatalanNumber: 16796, expectedError: nil},
{nthCatalanNumber: 1000, expectedCatalanNumber: 4233371109654655040, expectedError: nil},
}
for i := range testCatalanNumbersData {
t.Run(fmt.Sprintf("the %dth Catalan Number", testCatalanNumbersData[i].nthCatalanNumber), func(t *testing.T) {
nthCatalanNumber := testCatalanNumbersData[i].nthCatalanNumber
result, err := NthCatalanNumber(nthCatalanNumber)
expectedCatalanNumber := testCatalanNumbersData[i].expectedCatalanNumber
expectedError := testCatalanNumbersData[i].expectedError
if err != expectedError {
t.Errorf("Expected %dth Catalan Number error: %d\nFound: %d\n", nthCatalanNumber, expectedError, err)
}
if result != expectedCatalanNumber {
t.Errorf("Expected %dth Catalan Number: %d\nFound: %d\n", nthCatalanNumber, expectedCatalanNumber, result)
}
})
}
}
================================================
FILE: dynamic/coinchange.go
================================================
// coinchange.go
// description: Implementation of the coin change problem using dynamic programming
// details: The coin change problem is a problem that asks for the number of ways to make change for a given amount of money using a given set of coins. The problem can be solved using dynamic programming.
// time complexity: O(n*m) where n is the number of coins and m is the amount of money
// space complexity: O(m) where m is the amount of money
package dynamic
// CoinChange finds the number of possible combinations of coins
// of different values which can get to the target amount.
func CoinChange(coins []int32, amount int32) int32 {
combination := make([]int32, amount)
combination[0] = 1
for _, c := range coins {
for i := c; i < amount; i++ {
combination[i] += combination[i-c]
}
}
return combination[amount-1]
}
================================================
FILE: dynamic/coinchange_test.go
================================================
package dynamic_test
import (
"fmt"
"github.com/TheAlgorithms/Go/dynamic"
"testing"
)
func TestCoinChange(t *testing.T) {
coinCombination := []int32{1, 2, 5, 10}
targets := []struct {
target int32
expected int32
}{
{4, 2},
{5, 3},
{10, 8},
{15, 19},
{20, 34},
}
for _, v := range targets {
t.Run(fmt.Sprintf("target: %d ", v.target), func(t *testing.T) {
result := dynamic.CoinChange(coinCombination, v.target)
if result != v.expected {
t.Errorf("target: %d Expected %d, got %d", v.target, v.expected, result)
}
})
}
}
================================================
FILE: dynamic/dicethrow.go
================================================
// dicethrow.go
// description: Solves the Dice Throw Problem using dynamic programming
// reference: https://www.geeksforgeeks.org/dice-throw-problem/
// time complexity: O(m * n)
// space complexity: O(m * n)
package dynamic
// DiceThrow returns the number of ways to get sum `sum` using `m` dice with `n` faces
func DiceThrow(m, n, sum int) int {
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, sum+1)
}
for i := 1; i <= n; i++ {
if i <= sum {
dp[1][i] = 1
}
}
for i := 2; i <= m; i++ {
for j := 1; j <= sum; j++ {
for k := 1; k <= n; k++ {
if j-k >= 0 {
dp[i][j] += dp[i-1][j-k]
}
}
}
}
return dp[m][sum]
}
================================================
FILE: dynamic/dicethrow_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseDiceThrow struct {
numDice int
numFaces int
targetSum int
expected int
}
// getDiceThrowTestCases provides the test cases for DiceThrow
func getDiceThrowTestCases() []testCaseDiceThrow {
return []testCaseDiceThrow{
{2, 6, 7, 6}, // Two dice, six faces each, sum = 7
{1, 6, 3, 1}, // One die, six faces, sum = 3
{3, 4, 5, 6}, // Three dice, four faces each, sum = 5
{1, 6, 1, 1}, // One die, six faces, sum = 1
{2, 6, 12, 1}, // Two dice, six faces each, sum = 12
{3, 6, 18, 1}, // Three dice, six faces each, sum = 18
{2, 6, 20, 0}, // Two dice, six faces each, sum = 20 (impossible)
{1, 1, 1, 1}, // One die, one face, sum = 1
{1, 1, 2, 0}, // One die, one face, sum = 2 (impossible)
{2, 1, 2, 1}, // Two dice, one face each, sum = 2
}
}
// TestDiceThrow tests the DiceThrow function with basic test cases
func TestDiceThrow(t *testing.T) {
t.Run("Basic test cases", func(t *testing.T) {
for _, tc := range getDiceThrowTestCases() {
actual := dynamic.DiceThrow(tc.numDice, tc.numFaces, tc.targetSum)
if actual != tc.expected {
t.Errorf("DiceThrow(%d, %d, %d) = %d; expected %d", tc.numDice, tc.numFaces, tc.targetSum, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/doc.go
================================================
// Package dynamic is a package of certain implementations of dynamically run algorithms.
package dynamic
================================================
FILE: dynamic/dynamic_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package dynamic
================================================
FILE: dynamic/editdistance.go
================================================
// EDIT DISTANCE PROBLEM
// time complexity: O(m * n) where m and n are lengths of the strings, first and second respectively.
// space complexity: O(m * n) where m and n are lengths of the strings, first and second respectively.
// https://www.geeksforgeeks.org/edit-distance-dp-5/
// https://leetcode.com/problems/edit-distance/
package dynamic
import "github.com/TheAlgorithms/Go/math/min"
// EditDistanceRecursive is a naive implementation with exponential time complexity.
func EditDistanceRecursive(first string, second string, pointerFirst int, pointerSecond int) int {
if pointerFirst == 0 {
return pointerSecond
}
if pointerSecond == 0 {
return pointerFirst
}
// Characters match, so we recur for the remaining portions
if first[pointerFirst-1] == second[pointerSecond-1] {
return EditDistanceRecursive(first, second, pointerFirst-1, pointerSecond-1)
}
// We have three choices, all with cost of 1 unit
return 1 + min.Int(EditDistanceRecursive(first, second, pointerFirst, pointerSecond-1), // Insert
EditDistanceRecursive(first, second, pointerFirst-1, pointerSecond), // Delete
EditDistanceRecursive(first, second, pointerFirst-1, pointerSecond-1)) // Replace
}
// EditDistanceDP is an optimised implementation which builds on the ideas of the recursive implementation.
// We use dynamic programming to compute the DP table where dp[i][j] denotes the edit distance value
// of first[0..i-1] and second[0..j-1]. Time complexity is O(m * n) where m and n are lengths of the strings,
// first and second respectively.
func EditDistanceDP(first string, second string) int {
m := len(first)
n := len(second)
// Create the DP table
dp := make([][]int, m+1)
for i := 0; i <= m; i++ {
dp[i] = make([]int, n+1)
}
for i := 0; i <= m; i++ {
for j := 0; j <= n; j++ {
if i == 0 {
dp[i][j] = j
continue
}
if j == 0 {
dp[i][j] = i
continue
}
if first[i-1] == second[j-1] {
dp[i][j] = dp[i-1][j-1]
continue
}
dp[i][j] = 1 + min.Int(dp[i][j-1], dp[i-1][j], dp[i-1][j-1])
}
}
return dp[m][n]
}
================================================
FILE: dynamic/editdistance_test.go
================================================
package dynamic
import (
"fmt"
"testing"
)
func Test_EditDistance(t *testing.T) {
var testCases = []struct {
first string
second string
expected int
}{
{"", "", 0},
{"horse", "ros", 3},
{"intention", "execution", 5},
{"abcdxabcde", "abcdeabcdx", 2},
{"sunday", "saturday", 3},
{"food", "money", 4},
{"voldemort", "dumbledore", 7},
}
for i := range testCases {
t.Run(fmt.Sprintf("Word 1: %s, Word 2: %s", testCases[i].first, testCases[i].second), func(t *testing.T) {
computed := EditDistanceDP(testCases[i].first, testCases[i].second)
if computed != testCases[i].expected {
t.Errorf("Word 1: %s, Word 2: %s, Expected: %d, Computed: %d", testCases[i].first, testCases[i].second, testCases[i].expected, computed)
}
})
}
}
================================================
FILE: dynamic/eggdropping.go
================================================
package dynamic
import (
"github.com/TheAlgorithms/Go/math/max"
"github.com/TheAlgorithms/Go/math/min"
)
// EggDropping finds the minimum number of attempts needed to find the critical floor
// with `eggs` number of eggs and `floors` number of floors
func EggDropping(eggs, floors int) int {
// Edge case: If there are no floors, no attempts needed
if floors == 0 {
return 0
}
// Edge case: If there is one floor, one attempt needed
if floors == 1 {
return 1
}
// Edge case: If there is one egg, need to test all floors one by one
if eggs == 1 {
return floors
}
// Initialize DP table
dp := make([][]int, eggs+1)
for i := range dp {
dp[i] = make([]int, floors+1)
}
// Fill the DP table for 1 egg
for j := 1; j <= floors; j++ {
dp[1][j] = j
}
// Fill the DP table for more than 1 egg
for i := 2; i <= eggs; i++ {
for j := 2; j <= floors; j++ {
dp[i][j] = int(^uint(0) >> 1) // initialize with a large number
for x := 1; x <= j; x++ {
// Recurrence relation to fill the DP table
res := max.Int(dp[i-1][x-1], dp[i][j-x]) + 1
dp[i][j] = min.Int(dp[i][j], res)
}
}
}
return dp[eggs][floors]
}
================================================
FILE: dynamic/eggdropping_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseEggDropping struct {
eggs int
floors int
expected int
}
func getEggDroppingTestCases() []testCaseEggDropping {
return []testCaseEggDropping{
{1, 10, 10}, // One egg, need to test all floors
{2, 10, 4}, // Two eggs and ten floors
{3, 14, 4}, // Three eggs and fourteen floors
{2, 36, 8}, // Two eggs and thirty-six floors
{2, 0, 0}, // Two eggs, zero floors
}
}
func TestEggDropping(t *testing.T) {
t.Run("Egg Dropping test cases", func(t *testing.T) {
for _, tc := range getEggDroppingTestCases() {
actual := dynamic.EggDropping(tc.eggs, tc.floors)
if actual != tc.expected {
t.Errorf("EggDropping(%d, %d) = %d; expected %d", tc.eggs, tc.floors, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/fibonacci.go
================================================
// fibonacci.go
// description: Implementation of the Fibonacci sequence using dynamic programming
// time complexity: O(n)
// space complexity: O(1)
package dynamic
// https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/
// NthFibonacci returns the nth Fibonacci Number
func NthFibonacci(n uint) uint {
if n == 0 {
return 0
}
// n1 and n2 are the (i-1)th and ith Fibonacci numbers, respectively
var n1, n2 uint = 0, 1
for i := uint(1); i < n; i++ {
n3 := n1 + n2
n1 = n2
n2 = n3
}
return n2
}
================================================
FILE: dynamic/fibonacci_test.go
================================================
package dynamic
import (
"fmt"
"testing"
)
func Test_NthFibonacci(t *testing.T) {
// source: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html
var fibonacciNumbers = []struct {
nth uint
fibonacci uint
}{
{0, 0},
{1, 1},
{2, 1},
{3, 2},
{4, 3},
{5, 5},
{6, 8},
{7, 13},
{8, 21},
{9, 34},
{10, 55},
{20, 6765},
{30, 832040},
{40, 102334155},
{50, 12586269025},
{60, 1548008755920},
{70, 190392490709135},
{80, 23416728348467685},
{90, 2880067194370816120},
}
for i := range fibonacciNumbers {
t.Run(fmt.Sprintf("the %dth Fibonacci number", fibonacciNumbers[i].nth), func(t *testing.T) {
result := NthFibonacci(fibonacciNumbers[i].nth)
if result != fibonacciNumbers[i].fibonacci {
t.Errorf("Expected the %dth Fibonacci number to be %d, got %d", fibonacciNumbers[i].nth, fibonacciNumbers[i].fibonacci, result)
}
})
}
}
================================================
FILE: dynamic/interleavingstrings.go
================================================
// interleavingstrings.go
// description: Solves the Interleaving Strings problem using dynamic programming
// reference: https://en.wikipedia.org/wiki/Interleaving_strings
// time complexity: O(m*n)
// space complexity: O(m*n)
package dynamic
// IsInterleave checks if string `s1` and `s2` can be interleaved to form string `s3`
func IsInterleave(s1, s2, s3 string) bool {
if len(s1)+len(s2) != len(s3) {
return false
}
dp := make([][]bool, len(s1)+1)
for i := range dp {
dp[i] = make([]bool, len(s2)+1)
}
dp[0][0] = true
for i := 1; i <= len(s1); i++ {
dp[i][0] = dp[i-1][0] && s1[i-1] == s3[i-1]
}
for j := 1; j <= len(s2); j++ {
dp[0][j] = dp[0][j-1] && s2[j-1] == s3[j-1]
}
for i := 1; i <= len(s1); i++ {
for j := 1; j <= len(s2); j++ {
dp[i][j] = (dp[i-1][j] && s1[i-1] == s3[i+j-1]) || (dp[i][j-1] && s2[j-1] == s3[i+j-1])
}
}
return dp[len(s1)][len(s2)]
}
================================================
FILE: dynamic/interleavingstrings_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseInterleaving struct {
s1, s2, s3 string
expected bool
}
func getInterleavingTestCases() []testCaseInterleaving {
return []testCaseInterleaving{
{"aab", "axy", "aaxaby", true}, // Valid interleaving
{"aab", "axy", "abaaxy", false}, // Invalid interleaving
{"", "", "", true}, // All empty strings
{"abc", "", "abc", true}, // Only s1 matches s3
{"", "xyz", "xyz", true}, // Only s2 matches s3
{"abc", "xyz", "abxcyz", true}, // Valid interleaving
{"aaa", "aaa", "aaaaaa", true}, // Identical strings
{"aaa", "aaa", "aaaaaaa", false}, // Extra character
{"abc", "def", "abcdef", true}, // Concatenation order
{"abc", "def", "adbcef", true}, // Valid mixed interleaving
}
}
func TestIsInterleave(t *testing.T) {
t.Run("Interleaving Strings test cases", func(t *testing.T) {
for _, tc := range getInterleavingTestCases() {
actual := dynamic.IsInterleave(tc.s1, tc.s2, tc.s3)
if actual != tc.expected {
t.Errorf("IsInterleave(%q, %q, %q) = %v; expected %v", tc.s1, tc.s2, tc.s3, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/knapsack.go
================================================
package dynamic
// Knapsack Problem
// https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/
// https://en.wikipedia.org/wiki/Knapsack_problem
// time complexity: O(n*maxWeight)
// space complexity: O(n*maxWeight)
import (
"math"
)
// Max function - possible duplicate
func Max(a, b int) int {
return int(math.Max(float64(a), float64(b)))
}
// Knapsack solves knapsack problem
// return maxProfit
func Knapsack(maxWeight int, weights, values []int) int {
n := len(weights)
m := maxWeight
// create dp data structure
dp := make([][]int, n+1)
for i := range dp {
dp[i] = make([]int, m+1)
}
for i := 0; i < len(weights); i++ {
for j := 0; j <= maxWeight; j++ {
if weights[i] > j {
dp[i+1][j] = dp[i][j]
} else {
dp[i+1][j] = Max(dp[i][j-weights[i]]+values[i], dp[i][j])
}
}
}
return dp[n][m]
}
/*
func main() {
maxWeight := 50
values := []int{
60, 100, 120,
}
weights := []int{
10, 20, 30,
}
maxProfit := Knapsack(maxWeight, weights, values)
fmt.Println(maxProfit)
}
*/
================================================
FILE: dynamic/knapsack_test.go
================================================
package dynamic_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
func TestKnapsack(t *testing.T) {
td := []struct {
maxWeight int
weights []int
values []int
expected int
}{
{0, []int{0}, []int{0}, 0},
{10, []int{1, 2, 3}, []int{1, 1, 1}, 3}, // picks all
{10, []int{1, 2, 3, 4, 5, 6}, []int{1, 1, 1, 1, 1, 1}, 4}, // picks 1,2,3,4
{10, []int{1, 2, 3, 4, 5, 6}, []int{1, 1, 1, 1, 1, 5}, 7}, // picks 1,3,6
{10, []int{1, 2, 3, 4, 5, 6}, []int{-1, 10, -3, -4, 10, 1}, 20}, // picks 2,5
{10, []int{1, 2, 3, 4, 5, 6}, []int{-10, -10, -10, -10, 10, 10}, 10}, // picks 5 or 6
}
for _, tc := range td {
name := fmt.Sprintf("Knapsack problem with (maxWeight: %d, weights: %v, values: %v)", tc.maxWeight, tc.weights, tc.values)
t.Run(name, func(t *testing.T) {
actual := dynamic.Knapsack(tc.maxWeight, tc.weights, tc.values)
if actual != tc.expected {
t.Errorf("expecting knapsack with (maxWeight: %d, weights: %v, values: %v) to return %d but got %d", tc.maxWeight, tc.weights, tc.values, tc.expected, actual)
}
})
}
}
func ExampleKnapsack() {
fmt.Print(dynamic.Knapsack(10, []int{4, 5, 8}, []int{50, 15, 60}))
//Output:65
}
================================================
FILE: dynamic/longestarithmeticsubsequence.go
================================================
// longestarithmeticsubsequence.go
// description: Implementation of the Longest Arithmetic Subsequence problem
// reference: https://en.wikipedia.org/wiki/Longest_arithmetic_progression
// time complexity: O(n^2)
// space complexity: O(n^2)
package dynamic
// LongestArithmeticSubsequence returns the length of the longest arithmetic subsequence
func LongestArithmeticSubsequence(nums []int) int {
n := len(nums)
if n <= 1 {
return n
}
dp := make([]map[int]int, n)
for i := range dp {
dp[i] = make(map[int]int)
}
maxLength := 1
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
diff := nums[i] - nums[j]
dp[i][diff] = dp[j][diff] + 1
if dp[i][diff]+1 > maxLength {
maxLength = dp[i][diff] + 1
}
}
}
return maxLength
}
================================================
FILE: dynamic/longestarithmeticsubsequence_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseLongestArithmeticSubsequence struct {
nums []int
expected int
}
func getLongestArithmeticSubsequenceTestCases() []testCaseLongestArithmeticSubsequence {
return []testCaseLongestArithmeticSubsequence{
{[]int{3, 6, 9, 12}, 4}, // Arithmetic sequence of length 4
{[]int{9, 4, 7, 2, 10}, 3}, // Arithmetic sequence of length 3
{[]int{20, 1, 15, 3, 10, 5, 8}, 4}, // Arithmetic sequence of length 4
{[]int{1, 2, 3, 4, 5}, 5}, // Arithmetic sequence of length 5
{[]int{10, 7, 4, 1}, 4}, // Arithmetic sequence of length 4
{[]int{1, 5, 7, 8, 5, 3, 4, 3, 1, 2}, 4}, // Arithmetic sequence of length 4
{[]int{1, 3, 5, 7, 9}, 5}, // Arithmetic sequence of length 5
{[]int{5, 10, 15, 20}, 4}, // Arithmetic sequence of length 4
{[]int{1}, 1}, // Single element, length is 1
{[]int{}, 0}, // Empty array, length is 0
}
}
func TestLongestArithmeticSubsequence(t *testing.T) {
t.Run("Longest Arithmetic Subsequence test cases", func(t *testing.T) {
for _, tc := range getLongestArithmeticSubsequenceTestCases() {
actual := dynamic.LongestArithmeticSubsequence(tc.nums)
if actual != tc.expected {
t.Errorf("LongestArithmeticSubsequence(%v) = %v; expected %v", tc.nums, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/longestcommonsubsequence.go
================================================
// LONGEST COMMON SUBSEQUENCE
// DP - 4
// https://www.geeksforgeeks.org/longest-common-subsequence-dp-4/
// https://leetcode.com/problems/longest-common-subsequence/
// time complexity: O(m*n) where m and n are lengths of the strings
// space complexity: O(m*n) where m and n are lengths of the strings
package dynamic
func strToRuneSlice(s string) (r []rune, size int) {
r = []rune(s)
return r, len(r)
}
// LongestCommonSubsequence function
func LongestCommonSubsequence(a string, b string) int {
aRunes, aLen := strToRuneSlice(a)
bRunes, bLen := strToRuneSlice(b)
// here we are making a 2d slice of size (aLen+1)*(bLen+1)
lcs := make([][]int, aLen+1)
for i := 0; i <= aLen; i++ {
lcs[i] = make([]int, bLen+1)
}
// block that implements LCS
for i := 0; i <= aLen; i++ {
for j := 0; j <= bLen; j++ {
if i == 0 || j == 0 {
lcs[i][j] = 0
} else if aRunes[i-1] == bRunes[j-1] {
lcs[i][j] = lcs[i-1][j-1] + 1
} else {
lcs[i][j] = Max(lcs[i-1][j], lcs[i][j-1])
}
}
}
// returning the length of longest common subsequence
return lcs[aLen][bLen]
}
================================================
FILE: dynamic/longestcommonsubsequence_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseLCS struct {
stringA string
stringB string
expected int
}
func getLCSTestCases() []testCaseLCS {
return []testCaseLCS{
{"ABCDGH", "AEDFHR", 3},
{"AGGTAB", "GXTXAYB", 4},
{"programming", "gaming", 6},
{"physics", "smartphone", 2},
{"computer", "food", 1},
{"123", "12345", 3},
{"XYZ", "XYZ", 3},
{"XYZ", "XYZa", 3},
{"XYZ", "aXYZ", 3},
{"0123", "abc", 0},
{"abcdef", "aXbXcXXXdeXXf", 6},
{"", "abc", 0},
{"", "", 0},
{"££", "££", 2},
{"x笑x笑", "aaa笑a笑", 2},
{"xYxY", "aaaYaY", 2},
}
}
func TestLongestCommonSubsequence(t *testing.T) {
t.Run("Simple test", func(t *testing.T) {
for _, tc := range getLCSTestCases() {
actual := dynamic.LongestCommonSubsequence(tc.stringA, tc.stringB)
if actual != tc.expected {
t.Errorf("expected: %d, but got: %d", tc.expected, actual)
}
}
})
t.Run("Symmetry test", func(t *testing.T) {
for _, tc := range getLCSTestCases() {
actual := dynamic.LongestCommonSubsequence(tc.stringB, tc.stringA)
if actual != tc.expected {
t.Errorf("expected: %d, but got: %d", tc.expected, actual)
}
}
})
}
================================================
FILE: dynamic/longestincreasingsubsequence.go
================================================
// longestincreasingsubsequence.go
// description: Implementation of the Longest Increasing Subsequence using dynamic programming
// reference: https://en.wikipedia.org/wiki/Longest_increasing_subsequence
// time complexity: O(n^2)
// space complexity: O(n)
package dynamic
import (
"github.com/TheAlgorithms/Go/math/max"
)
// LongestIncreasingSubsequence returns the longest increasing subsequence
// where all elements of the subsequence are sorted in increasing order
func LongestIncreasingSubsequence(elements []int) int {
n := len(elements)
lis := make([]int, n)
for i := range lis {
lis[i] = 1
}
for i := range lis {
for j := 0; j < i; j++ {
if elements[i] > elements[j] && lis[i] < lis[j]+1 {
lis[i] = lis[j] + 1
}
}
}
res := 0
for _, value := range lis {
res = max.Int(res, value)
}
return res
}
================================================
FILE: dynamic/longestincreasingsubsequence_test.go
================================================
package dynamic_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
func longestIncreasingSubsequenceTest(t *testing.T, algorithm func(nums []int) int) {
td := []struct {
elements []int
expectedLen int
}{
{[]int{1, 2, 3, 4, 5, 10}, 6},
{[]int{1, 7, 3, 4, 5}, 4}, // 1,3,4,5
{[]int{1, 3, 5}, 3},
{[]int{7, 1, 6}, 2},
{[]int{4, 1, 6, 2}, 2},
{[]int{11, 9, 6}, 1},
}
for _, tc := range td {
t.Run(fmt.Sprint("test with", tc.elements), func(t *testing.T) {
actualLen := algorithm(tc.elements)
if tc.expectedLen != actualLen {
t.Fatalf("expecting a sequence of len %d to be found but the actual len was %d; input: %v", tc.expectedLen, actualLen, tc.elements)
}
})
}
}
func TestLongestIncreasingSubsequence(t *testing.T) {
longestIncreasingSubsequenceTest(t, dynamic.LongestIncreasingSubsequence)
}
func TestLongestIncreasingSubsequenceGreedy(t *testing.T) {
longestIncreasingSubsequenceTest(t, dynamic.LongestIncreasingSubsequenceGreedy)
}
================================================
FILE: dynamic/longestincreasingsubsequencegreedy.go
================================================
package dynamic
// LongestIncreasingSubsequenceGreedy is a function to find the longest increasing
// subsequence in a given array using a greedy approach.
// The dynamic programming approach is implemented alongside this one.
// Worst Case Time Complexity: O(nlogn)
// Auxiliary Space: O(n), where n is the length of the array(slice).
// Reference: https://www.geeksforgeeks.org/construction-of-longest-monotonically-increasing-subsequence-n-log-n/
func LongestIncreasingSubsequenceGreedy(nums []int) int {
longestIncreasingSubsequence := make([]int, 0)
for _, num := range nums {
// find the leftmost index in longestIncreasingSubsequence with value >= num
leftmostIndex := lowerBound(longestIncreasingSubsequence, num)
if leftmostIndex == len(longestIncreasingSubsequence) {
longestIncreasingSubsequence = append(longestIncreasingSubsequence, num)
} else {
longestIncreasingSubsequence[leftmostIndex] = num
}
}
return len(longestIncreasingSubsequence)
}
// Function to find the leftmost index in arr with value >= val, mimicking the inbuild lower_bound function in C++
// Time Complexity: O(logn)
// Auxiliary Space: O(1)
func lowerBound(arr []int, val int) int {
searchWindowLeft, searchWindowRight := 0, len(arr)-1
for searchWindowLeft <= searchWindowRight {
middle := (searchWindowLeft + searchWindowRight) / 2
if arr[middle] < val {
searchWindowLeft = middle + 1
} else {
searchWindowRight = middle - 1
}
}
return searchWindowRight + 1
}
================================================
FILE: dynamic/longestpalindromicsubsequence.go
================================================
// longest palindromic subsequence
// time complexity: O(n^2)
// space complexity: O(n^2)
// http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/
package dynamic
func lpsRec(word string, i, j int) int {
if i == j {
return 1
}
if i > j {
return 0
}
if word[i] == word[j] {
return 2 + lpsRec(word, i+1, j-1)
}
return Max(lpsRec(word, i, j-1), lpsRec(word, i+1, j))
}
// LpsRec function
func LpsRec(word string) int {
return lpsRec(word, 0, len(word)-1)
}
// LpsDp function
func LpsDp(word string) int {
N := len(word)
dp := make([][]int, N)
for i := 0; i < N; i++ {
dp[i] = make([]int, N)
dp[i][i] = 1
}
for l := 2; l <= N; l++ {
// for length l
for i := 0; i < N-l+1; i++ {
j := i + l - 1
if word[i] == word[j] {
if l == 2 {
dp[i][j] = 2
} else {
dp[i][j] = 2 + dp[i+1][j-1]
}
} else {
dp[i][j] = Max(dp[i+1][j], dp[i][j-1])
}
}
}
return dp[0][N-1]
}
================================================
FILE: dynamic/longestpalindromicsubsequence_test.go
================================================
package dynamic_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
func lpsTestTemplate(t *testing.T, algorithm func(input string) int) {
testCases := []struct {
input string
expected int
}{
{"BBABCBCAB", 7},
{"BBBAB", 4},
{"ABBD", 2},
{"GEEKSFORGEEKS", 5},
{"abcdefgh", 1},
{"bbbab", 4},
{"cbbd", 2},
{"racexyzcxar", 7},
}
for _, tc := range testCases {
t.Run(fmt.Sprint("test with ", tc.input), func(t *testing.T) {
result := algorithm(tc.input)
if tc.expected != result {
t.Fatalf("expected %d, got %d", tc.expected, result)
}
})
}
}
func TestLpsRec(t *testing.T) {
lpsTestTemplate(t, dynamic.LpsRec)
}
func TestLpsDp(t *testing.T) {
lpsTestTemplate(t, dynamic.LpsDp)
}
================================================
FILE: dynamic/longestpalindromicsubstring.go
================================================
// longestpalindromicsubstring.go
// description: Implementation of finding the longest palindromic substring
// reference: https://en.wikipedia.org/wiki/Longest_palindromic_substring
// time complexity: O(n^2)
// space complexity: O(n^2)
package dynamic
// LongestPalindromicSubstring returns the longest palindromic substring in the input string
func LongestPalindromicSubstring(s string) string {
n := len(s)
if n == 0 {
return ""
}
dp := make([][]bool, n)
for i := range dp {
dp[i] = make([]bool, n)
}
start := 0
maxLength := 1
for i := 0; i < n; i++ {
dp[i][i] = true
}
for length := 2; length <= n; length++ {
for i := 0; i < n-length+1; i++ {
j := i + length - 1
if length == 2 {
dp[i][j] = (s[i] == s[j])
} else {
dp[i][j] = (s[i] == s[j]) && dp[i+1][j-1]
}
if dp[i][j] && length > maxLength {
maxLength = length
start = i
}
}
}
return s[start : start+maxLength]
}
================================================
FILE: dynamic/longestpalindromicsubstring_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseLongestPalindromicSubstring struct {
s string
expected string
}
func getLongestPalindromicSubstringTestCases() []testCaseLongestPalindromicSubstring {
return []testCaseLongestPalindromicSubstring{
{"babad", "bab"}, // Example with multiple palindromes
{"cbbd", "bb"}, // Example with longest even palindrome
{"a", "a"}, // Single character, palindrome is itself
{"", ""}, // Empty string, no palindrome
{"racecar", "racecar"}, // Whole string is a palindrome
{"abcba", "abcba"}, // Palindrome in the middle
{"aabbcc", "aa"}, // Multiple substrings, longest "aa"
{"madam", "madam"}, // Full palindrome string
{"forgeeksskeegfor", "geeksskeeg"}, // Complex palindrome in the middle
}
}
func TestLongestPalindromicSubstring(t *testing.T) {
t.Run("Longest Palindromic Substring test cases", func(t *testing.T) {
for _, tc := range getLongestPalindromicSubstringTestCases() {
actual := dynamic.LongestPalindromicSubstring(tc.s)
if actual != tc.expected {
t.Errorf("LongestPalindromicSubstring(%q) = %q; expected %q", tc.s, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/matrixmultiplication.go
================================================
// matrix chain multiplication problem
// https://en.wikipedia.org/wiki/Matrix_chain_multiplication
// www.geeksforgeeks.org/dynamic_programming-set-8-matrix-chain-multiplication/
// time complexity: O(n^3)
// space complexity: O(n^2)
package dynamic
import "github.com/TheAlgorithms/Go/math/min"
// MatrixChainRec function
func MatrixChainRec(D []int, i, j int) int {
// d[i-1] x d[i] : dimension of matrix i
if i == j {
return 0
}
q := 1 << 32
for k := i; k < j; k++ {
prod := MatrixChainRec(D, i, k) + MatrixChainRec(D, k+1, j) + D[i-1]*D[k]*D[j]
q = min.Int(prod, q)
}
return q
}
// MatrixChainDp function
func MatrixChainDp(D []int) int {
// d[i-1] x d[i] : dimension of matrix i
N := len(D)
dp := make([][]int, N) // dp[i][j] = matrixChainRec(D, i, j)
for i := 0; i < N; i++ {
dp[i] = make([]int, N)
dp[i][i] = 0
}
for l := 2; l < N; l++ {
for i := 1; i < N-l+1; i++ {
j := i + l - 1
dp[i][j] = 1 << 31
for k := i; k < j; k++ {
prod := dp[i][k] + dp[k+1][j] + D[i-1]*D[k]*D[j]
dp[i][j] = min.Int(prod, dp[i][j])
}
}
}
return dp[1][N-1]
}
/*
func main() {
D := []int{2, 2, 2, 2, 2} // 4 matrices
fmt.Print(matrixChainRec(D, 1, 4), "\n")
fmt.Print(matrixChainDp(D), "\n")
}
*/
================================================
FILE: dynamic/maxsubarraysum.go
================================================
// maxsubarraysum.go
// description: Implementation of Kadane's algorithm for Maximum Subarray Sum
// reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem
// time complexity: O(n)
// space complexity: O(1)
package dynamic
import "github.com/TheAlgorithms/Go/math/max"
// MaxSubArraySum returns the sum of the maximum subarray in the input array
func MaxSubArraySum(nums []int) int {
maxSum := nums[0]
currentSum := nums[0]
for i := 1; i < len(nums); i++ {
currentSum = max.Int(nums[i], currentSum+nums[i])
maxSum = max.Int(maxSum, currentSum)
}
return maxSum
}
================================================
FILE: dynamic/maxsubarraysum_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseMaxSubArraySum struct {
nums []int
expected int
}
func getMaxSubArraySumTestCases() []testCaseMaxSubArraySum {
return []testCaseMaxSubArraySum{
{[]int{-2, -3, 4, -1, -2, 1, 5, -3}, 7}, // Kadane's algorithm example
{[]int{-1, -2, -3, -4}, -1}, // All negative numbers, max single element
{[]int{5, 4, -1, 7, 8}, 23}, // Positive numbers with a large sum
{[]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}, 6}, // Mixed with a maximum subarray of length 4
{[]int{1, 2, 3, 4, 5}, 15}, // All positive numbers, sum is the entire array
{[]int{-1, -2, -3, -4, -5}, -1}, // Only negative numbers, largest single element
{[]int{0, 0, 0, 0, 0}, 0}, // Array of zeros, maximum subarray is zero
{[]int{3}, 3}, // Single positive number
{[]int{-1}, -1}, // Single negative number
}
}
func TestMaxSubArraySum(t *testing.T) {
t.Run("Max SubArray Sum test cases", func(t *testing.T) {
for _, tc := range getMaxSubArraySumTestCases() {
actual := dynamic.MaxSubArraySum(tc.nums)
if actual != tc.expected {
t.Errorf("MaxSubArraySum(%v) = %v; expected %v", tc.nums, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/optimalbst.go
================================================
package dynamic
import "github.com/TheAlgorithms/Go/math/min"
// OptimalBST returns the minimum cost of constructing a Binary Search Tree
func OptimalBST(keys []int, freq []int, n int) int {
// Initialize DP table with size n x n
dp := make([][]int, n)
for i := range dp {
dp[i] = make([]int, n)
}
// Base case: single key cost
for i := 0; i < n; i++ {
dp[i][i] = freq[i]
}
// Build the DP table for sequences of length 2 to n
for length := 2; length <= n; length++ {
for i := 0; i < n-length+1; i++ {
j := i + length - 1
dp[i][j] = int(^uint(0) >> 1) // Initialize with a large value
sum := sum(freq, i, j)
// Try every key as root and compute cost
for k := i; k <= j; k++ {
// Left cost: dp[i][k-1] is valid only if k > i
var leftCost int
if k > i {
leftCost = dp[i][k-1]
} else {
leftCost = 0
}
// Right cost: dp[k+1][j] is valid only if k < j
var rightCost int
if k < j {
rightCost = dp[k+1][j]
} else {
rightCost = 0
}
// Total cost for root k
cost := sum + leftCost + rightCost
// Update dp[i][j] with the minimum cost
dp[i][j] = min.Int(dp[i][j], cost)
}
}
}
return dp[0][n-1]
}
// Helper function to sum the frequencies
func sum(freq []int, i, j int) int {
total := 0
for k := i; k <= j; k++ {
total += freq[k]
}
return total
}
================================================
FILE: dynamic/optimalbst_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseOptimalBST struct {
keys []int
freq []int
n int
expected int
}
func getOptimalBSTTestCases() []testCaseOptimalBST {
return []testCaseOptimalBST{
{[]int{10, 12, 20}, []int{34, 8, 50}, 3, 142}, // Example with 3 keys
{[]int{10, 20, 30, 40, 50}, []int{10, 20, 30, 40, 50}, 5, 300}, // Example with 5 keys
{[]int{10}, []int{100}, 1, 100}, // Single key case
}
}
func TestOptimalBST(t *testing.T) {
t.Run("Optimal Binary Search Tree test cases", func(t *testing.T) {
for _, tc := range getOptimalBSTTestCases() {
t.Run("testing optimal BST", func(t *testing.T) {
actual := dynamic.OptimalBST(tc.keys, tc.freq, tc.n)
if actual != tc.expected {
t.Errorf("OptimalBST(%v, %v, %d) = %d; expected %d", tc.keys, tc.freq, tc.n, actual, tc.expected)
}
})
}
})
}
================================================
FILE: dynamic/partitionproblem.go
================================================
// partitionproblem.go
// description: Solves the Partition Problem using dynamic programming
// reference: https://en.wikipedia.org/wiki/Partition_problem
// time complexity: O(n*sum)
// space complexity: O(n*sum)
package dynamic
// PartitionProblem checks whether the given set can be partitioned into two subsets
// such that the sum of the elements in both subsets is the same.
func PartitionProblem(nums []int) bool {
sum := 0
for _, num := range nums {
sum += num
}
if sum%2 != 0 {
return false
}
target := sum / 2
dp := make([]bool, target+1)
dp[0] = true
for _, num := range nums {
for i := target; i >= num; i-- {
dp[i] = dp[i] || dp[i-num]
}
}
return dp[target]
}
================================================
FILE: dynamic/partitionproblem_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
// testCasePartitionProblem holds the test cases for the Partition Problem
type testCasePartitionProblem struct {
nums []int
expected bool
}
// getPartitionProblemTestCases returns a list of test cases for the Partition Problem
func getPartitionProblemTestCases() []testCasePartitionProblem {
return []testCasePartitionProblem{
{[]int{1, 5, 11, 5}, true}, // Example with a partitionable set
{[]int{1, 2, 3, 5}, false}, // Example where partition is not possible
{[]int{1, 2, 5}, false}, // Set cannot be partitioned into two subsets
{[]int{2, 2, 2, 2}, true}, // Even split possible with equal elements
{[]int{7, 3, 2, 1}, false}, // Set cannot be partitioned
{[]int{}, true}, // Empty set, can be partitioned trivially
{[]int{1}, false}, // Single element, cannot be partitioned
{[]int{10, 10, 10, 10}, true}, // Equal elements, partitionable
}
}
// TestPartitionProblem tests the PartitionProblem function with different test cases
func TestPartitionProblem(t *testing.T) {
t.Run("Partition Problem test cases", func(t *testing.T) {
for _, tc := range getPartitionProblemTestCases() {
actual := dynamic.PartitionProblem(tc.nums)
if actual != tc.expected {
t.Errorf("PartitionProblem(%v) = %v; expected %v", tc.nums, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/rodcutting.go
================================================
// Solution to Rod cutting problem
// https://en.wikipedia.org/wiki/Cutting_stock_problem
// http://www.geeksforgeeks.org/dynamic-programming-set-13-cutting-a-rod/
// time complexity: O(n^2)
// space complexity: O(n)
package dynamic
// CutRodRec solve the problem recursively: initial approach
func CutRodRec(price []int, length int) int {
if length == 0 {
return 0
}
q := -1
for i := 1; i <= length; i++ {
q = Max(q, price[i]+CutRodRec(price, length-i))
}
return q
}
// CutRodDp solve the same problem using dynamic programming
func CutRodDp(price []int, length int) int {
r := make([]int, length+1) // a.k.a the memoization array
r[0] = 0 // cost of 0 length rod is 0
for j := 1; j <= length; j++ { // for each length (subproblem)
q := -1
for i := 1; i <= j; i++ {
q = Max(q, price[i]+r[j-i]) // avoiding recursive call
}
r[j] = q
}
return r[length]
}
/*
func main() {
length := 10
price := []int{0, 1, 5, 8, 9, 17, 17, 17, 20, 24, 30}
// price := []int{0, 10, 5, 8, 9, 17, 17, 17, 20, 24, 30}
// fmt.Print(price[5]+price[length-5], "\n")
fmt.Print(cutRodRec(price, length), "\n")
fmt.Print(cutRodDp(price, length), "\n")
}
*/
================================================
FILE: dynamic/rodcutting_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type rodCuttingTestCase struct {
price []int
length int
expected int
}
func getRodCuttingTestCases() []rodCuttingTestCase {
return []rodCuttingTestCase{
{[]int{0, 1, 5, 8, 9}, 4, 10},
{[]int{0, 2, 5, 7, 8, 0}, 5, 12},
{[]int{0, 1, 5, 8, 9, 10, 17, 17, 20}, 8, 22},
{[]int{0, 3, 5, 8, 9, 10, 17, 17, 20}, 8, 24},
}
}
func cutRodSolTestFunc(t *testing.T, cutRodSolFunc func([]int, int) int) {
for _, tc := range getRodCuttingTestCases() {
actual := cutRodSolFunc(tc.price, tc.length)
if actual != tc.expected {
t.Errorf("expected: %d, got: %d", tc.expected, actual)
}
}
}
func TestCutRodRec(t *testing.T) {
cutRodSolTestFunc(t, dynamic.CutRodRec)
}
func TestCutRodDp(t *testing.T) {
cutRodSolTestFunc(t, dynamic.CutRodDp)
}
================================================
FILE: dynamic/subsetsum.go
================================================
//Given a set of non-negative integers, and a (positive) value sum,
//determine if there is a subset of the given set with sum
//equal to given sum.
// time complexity: O(n*sum)
// space complexity: O(n*sum)
//references: https://www.geeksforgeeks.org/subset-sum-problem-dp-25/
package dynamic
import "fmt"
var ErrInvalidPosition = fmt.Errorf("invalid position in subset")
var ErrNegativeSum = fmt.Errorf("negative sum is not allowed")
func IsSubsetSum(array []int, sum int) (bool, error) {
if sum < 0 {
//not allow negative sum
return false, ErrNegativeSum
}
//create subset matrix
arraySize := len(array)
subset := make([][]bool, arraySize+1)
for i := 0; i <= arraySize; i++ {
subset[i] = make([]bool, sum+1)
}
for i := 0; i <= arraySize; i++ {
//sum 0 is always true
subset[i][0] = true
}
for i := 1; i <= sum; i++ {
//empty set is false when sum is not 0
subset[0][i] = false
}
for i := 1; i <= arraySize; i++ {
for j := 1; j <= sum; j++ {
if array[i-1] > j {
subset[i][j] = subset[i-1][j]
}
if array[i-1] <= j {
if j-array[i-1] < 0 || j-array[i-1] > sum {
//out of bounds
return false, ErrInvalidPosition
}
subset[i][j] = subset[i-1][j] || subset[i-1][j-array[i-1]]
}
}
}
return subset[arraySize][sum], nil
}
================================================
FILE: dynamic/subsetsum_test.go
================================================
package dynamic
import "testing"
func TestSubsetSum(t *testing.T) {
var subsetSumTestData = []struct {
description string
array []int
sum int
expectedResult bool
expectedError error
}{
{
description: "array of size 0 and sum 0",
array: []int{},
sum: 0,
expectedResult: true,
expectedError: nil,
},
{
description: "array of size 0 and non-zero sum",
array: []int{},
sum: 2,
expectedResult: false,
expectedError: nil,
},
{
description: "array of size 2 and sum 0",
array: []int{1, 2},
sum: 0,
expectedResult: true,
expectedError: nil,
},
{
description: "array of size 5 and sum 6",
array: []int{1, 2, 3, 4, 5},
sum: 6,
expectedResult: true,
expectedError: nil,
},
{
description: "array of size 5 and sum 7",
array: []int{1, 2, 3, 4, 5},
sum: 7,
expectedResult: true,
expectedError: nil,
},
{
description: "array of size 5 and sum 17",
array: []int{1, 2, 3, 4, 5},
sum: 17,
expectedResult: false,
expectedError: nil,
},
{
description: "array of size 6 negative values and sum positive",
array: []int{-1, -2, -3, -4, -5, -6},
sum: 1,
expectedResult: false,
expectedError: ErrInvalidPosition,
},
{
description: "array of size 6 with positive and negative values, also with a negative sum",
array: []int{-1, -1, 3, 4, 5, 6},
sum: -2,
expectedResult: false,
expectedError: ErrNegativeSum,
},
}
for i := range subsetSumTestData {
t.Run(subsetSumTestData[i].description, func(t *testing.T) {
array := subsetSumTestData[i].array
sum := subsetSumTestData[i].sum
expectedResult := subsetSumTestData[i].expectedResult
result, err := IsSubsetSum(array, sum)
expectedError := subsetSumTestData[i].expectedError
if err != expectedError {
t.Logf("FAIL: %s", subsetSumTestData[i].description)
t.Fatalf("Expected error:%t\nFound: %t", expectedError, err)
}
if result != expectedResult {
t.Logf("FAIL: %s", subsetSumTestData[i].description)
t.Fatalf("Expected result:%t\nFound: %t", expectedResult, result)
}
})
}
}
================================================
FILE: dynamic/tilingproblem.go
================================================
// tilingproblem.go
// description: Solves the Tiling Problem using dynamic programming
// reference: https://en.wikipedia.org/wiki/Tiling_problem
// time complexity: O(n)
// space complexity: O(n)
package dynamic
// TilingProblem returns the number of ways to tile a 2xN grid using 2x1 dominoes
func TilingProblem(n int) int {
if n <= 1 {
return 1
}
dp := make([]int, n+1)
dp[0] = 1
dp[1] = 1
for i := 2; i <= n; i++ {
dp[i] = dp[i-1] + dp[i-2]
}
return dp[n]
}
================================================
FILE: dynamic/tilingproblem_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseTilingProblem struct {
n int
expected int
}
func getTilingProblemTestCases() []testCaseTilingProblem {
return []testCaseTilingProblem{
{1, 1}, // Base case: 1 way to tile a 2x1 grid
{2, 2}, // 2 ways to tile a 2x2 grid
{3, 3}, // 3 ways to tile a 2x3 grid
{4, 5}, // 5 ways to tile a 2x4 grid
{5, 8}, // 8 ways to tile a 2x5 grid
{6, 13}, // 13 ways to tile a 2x6 grid
{10, 89}, // 89 ways to tile a 2x10 grid
{0, 1}, // Edge case: 1 way to tile a 2x0 grid (no tiles)
{7, 21}, // 21 ways to tile a 2x7 grid
{8, 34}, // 34 ways to tile a 2x8 grid
}
}
func TestTilingProblem(t *testing.T) {
t.Run("Tiling Problem test cases", func(t *testing.T) {
for _, tc := range getTilingProblemTestCases() {
actual := dynamic.TilingProblem(tc.n)
if actual != tc.expected {
t.Errorf("TilingProblem(%d) = %d; expected %d", tc.n, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/traprainwater.go
================================================
// filename: traprainwater.go
// description: Provides a function to calculate the amount of trapped rainwater between bars represented by an elevation map using dynamic programming.
// details:
// The TrapRainWater function calculates the amount of trapped rainwater between the bars represented by the given elevation map.
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
// Then, it iterates through the array to calculate the amount of trapped rainwater at each position based on the minimum of the left and right maximum heights.
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
// time complexity: O(n)
// space complexity: O(n)
// author(s) [TruongNhanNguyen (SOZEL)](https://github.com/TruongNhanNguyen)
package dynamic
import "math"
// TrapRainWater calculates the amount of trapped rainwater between the bars represented by the given elevation map.
// It uses dynamic programming to precompute the maximum height of bars to the left and right of each position.
// Then, it iterates through the array to calculate the amount of trapped rainwater at each position based on the minimum of the left and right maximum heights.
// Finally, it sums up the trapped rainwater for all positions and returns the total amount.
func TrapRainWater(height []int) int {
if len(height) == 0 {
return 0
}
leftMax := make([]int, len(height))
rightMax := make([]int, len(height))
leftMax[0] = height[0]
for i := 1; i < len(height); i++ {
leftMax[i] = int(math.Max(float64(leftMax[i-1]), float64(height[i])))
}
rightMax[len(height)-1] = height[len(height)-1]
for i := len(height) - 2; i >= 0; i-- {
rightMax[i] = int(math.Max(float64(rightMax[i+1]), float64(height[i])))
}
trappedWater := 0
for i := 0; i < len(height); i++ {
trappedWater += int(math.Min(float64(leftMax[i]), float64(rightMax[i]))) - height[i]
}
return trappedWater
}
================================================
FILE: dynamic/traprainwater_test.go
================================================
package dynamic_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
func TestTrapRainWater(t *testing.T) {
heights := [][]int{
{},
{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1},
{4, 2, 0, 3, 2, 5},
{3, 1, 2, 4, 0, 1, 3, 2, 4},
}
expectedResults := []int{
0,
6,
9,
13,
}
for i, height := range heights {
expected := expectedResults[i]
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
result := dynamic.TrapRainWater(height)
if result != expected {
t.Errorf("Expected %d, but got %d", expected, result)
}
})
}
}
================================================
FILE: dynamic/uniquepaths.go
================================================
// See https://leetcode.com/problems/unique-paths/
// time complexity: O(m*n) where m and n are the dimensions of the grid
// space complexity: O(m*n) where m and n are the dimensions of the grid
// author: Rares Mateizer (https://github.com/rares985)
package dynamic
// UniquePaths implements the solution to the "Unique Paths" problem
func UniquePaths(m, n int) int {
if m <= 0 || n <= 0 {
return 0
}
grid := make([][]int, m)
for i := range grid {
grid[i] = make([]int, n)
}
for i := 0; i < m; i++ {
grid[i][0] = 1
}
for j := 0; j < n; j++ {
grid[0][j] = 1
}
for i := 1; i < m; i++ {
for j := 1; j < n; j++ {
grid[i][j] = grid[i-1][j] + grid[i][j-1]
}
}
return grid[m-1][n-1]
}
================================================
FILE: dynamic/uniquepaths_test.go
================================================
package dynamic
import (
"testing"
)
func TestUniquePaths(t *testing.T) {
testCases := map[string]struct {
m int
n int
want int
}{
"negative sizes": {-1, -1, 0},
"empty matrix both dimensions": {0, 0, 0},
"empty matrix one dimension": {0, 1, 0},
"one element": {1, 1, 1},
"small matrix": {2, 2, 2},
"stress test": {1000, 1000, 2874513998398909184},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
if got := UniquePaths(test.m, test.n); got != test.want {
t.Errorf("UniquePaths(%v, %v) = %v, want %v", test.m, test.n, got, test.want)
}
})
}
}
================================================
FILE: dynamic/wildcardmatching.go
================================================
// wildcardmatching.go
// description: Solves the Wildcard Matching problem using dynamic programming
// reference: https://en.wikipedia.org/wiki/Wildcard_matching
// time complexity: O(m*n)
// space complexity: O(m*n)
package dynamic
// IsMatch checks if the string `s` matches the wildcard pattern `p`
func IsMatch(s, p string) bool {
dp := make([][]bool, len(s)+1)
for i := range dp {
dp[i] = make([]bool, len(p)+1)
}
dp[0][0] = true
for j := 1; j <= len(p); j++ {
if p[j-1] == '*' {
dp[0][j] = dp[0][j-1]
}
}
for i := 1; i <= len(s); i++ {
for j := 1; j <= len(p); j++ {
if p[j-1] == s[i-1] || p[j-1] == '?' {
dp[i][j] = dp[i-1][j-1]
} else if p[j-1] == '*' {
dp[i][j] = dp[i-1][j] || dp[i][j-1]
}
}
}
return dp[len(s)][len(p)]
}
================================================
FILE: dynamic/wildcardmatching_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
// testCaseWildcardMatching holds the test cases for the Wildcard Matching problem
type testCaseWildcardMatching struct {
s string
p string
expected bool
}
// getWildcardMatchingTestCases returns a list of test cases for the Wildcard Matching problem
func getWildcardMatchingTestCases() []testCaseWildcardMatching {
return []testCaseWildcardMatching{
{"aa", "a*", true}, // '*' can match zero or more characters
{"aa", "a", false}, // No match due to no wildcard
{"ab", "?*", true}, // '?' matches any single character, '*' matches remaining
{"abcd", "a*d", true}, // '*' matches the characters between 'a' and 'd'
{"abcd", "a*c", false}, // No match as 'c' doesn't match the last character 'd'
{"abc", "*", true}, // '*' matches the entire string
{"abc", "a*c", true}, // '*' matches 'b'
{"abc", "a?c", true}, // '?' matches 'b'
{"abc", "a?d", false}, // '?' cannot match 'd'
{"", "", true}, // Both strings empty, so they match
{"a", "?", true}, // '?' matches any single character
{"a", "*", true}, // '*' matches any number of characters, including one
}
}
// TestIsMatch tests the IsMatch function with various test cases
func TestIsMatch(t *testing.T) {
t.Run("Wildcard Matching test cases", func(t *testing.T) {
for _, tc := range getWildcardMatchingTestCases() {
actual := dynamic.IsMatch(tc.s, tc.p)
if actual != tc.expected {
t.Errorf("IsMatch(%q, %q) = %v; expected %v", tc.s, tc.p, actual, tc.expected)
}
}
})
}
================================================
FILE: dynamic/wordbreak.go
================================================
// wordbreak.go
// description: Solves the Word Break Problem using dynamic programming
// reference: https://en.wikipedia.org/wiki/Word_break_problem
// time complexity: O(n^2)
// space complexity: O(n)
package dynamic
// WordBreak checks if the input string can be segmented into words from a dictionary
func WordBreak(s string, wordDict []string) bool {
wordSet := make(map[string]bool)
for _, word := range wordDict {
wordSet[word] = true
}
dp := make([]bool, len(s)+1)
dp[0] = true
for i := 1; i <= len(s); i++ {
for j := 0; j < i; j++ {
if dp[j] && wordSet[s[j:i]] {
dp[i] = true
break
}
}
}
return dp[len(s)]
}
================================================
FILE: dynamic/wordbreak_test.go
================================================
package dynamic_test
import (
"testing"
"github.com/TheAlgorithms/Go/dynamic"
)
type testCaseWordBreak struct {
s string
wordDict []string
expected bool
}
func getWordBreakTestCases() []testCaseWordBreak {
return []testCaseWordBreak{
{"leetcode", []string{"leet", "code"}, true}, // "leetcode" can be segmented into "leet" and "code"
{"applepenapple", []string{"apple", "pen"}, true}, // "applepenapple" can be segmented into "apple", "pen", "apple"
{"catsanddog", []string{"cats", "dog", "sand", "and", "cat"}, true}, // "catsanddog" can be segmented into "cats", "and", "dog"
{"bb", []string{"a", "b", "bbb", "aaaa", "aaa"}, true}, // "bb" can be segmented into "b" and "b"
{"", []string{"cat", "dog", "sand", "and"}, true}, // Empty string can always be segmented (empty words)
{"applepie", []string{"apple", "pie"}, true}, // "applepie" can be segmented into "apple" and "pie"
{"catsandog", []string{"cats", "dog", "sand", "and", "cat"}, false}, // "catsandog" cannot be segmented
{"ilovecoding", []string{"i", "love", "coding"}, true}, // "ilovecoding" can be segmented into "i", "love", "coding"
{"cars", []string{"car", "ca", "rs"}, true}, // "cars" can be segmented into "car" and "s"
{"pen", []string{"pen", "pencil"}, true}, // "pen" is a direct match
{"apple", []string{"orange", "banana"}, false}, // "apple" is not in the word dictionary
}
}
func TestWordBreak(t *testing.T) {
t.Run("Word Break test cases", func(t *testing.T) {
for _, tc := range getWordBreakTestCases() {
actual := dynamic.WordBreak(tc.s, tc.wordDict)
if actual != tc.expected {
t.Errorf("WordBreak(%q, %v) = %v; expected %v", tc.s, tc.wordDict, actual, tc.expected)
}
}
})
}
================================================
FILE: go.mod
================================================
module github.com/TheAlgorithms/Go
go 1.19
================================================
FILE: go.sum
================================================
================================================
FILE: graph/articulationpoints.go
================================================
// Package graph provides algorithms to analyze graph structures.
package graph
import "github.com/TheAlgorithms/Go/math/min"
// apHelper stores auxiliary data used to identify articulation points in a graph.
type apHelper struct {
isAP []bool
visited []bool
childCount []int
discoveryTime []int
earliestDiscovery []int
}
// ArticulationPoint identifies articulation points in a graph. It returns a boolean slice
// where each element indicates whether a vertex is an articulation point.
// Worst Case Time Complexity: O(|V| + |E|)
// Auxiliary Space: O(|V|)
// Reference: https://en.wikipedia.org/wiki/Biconnected_component and https://cptalks.quora.com/Cut-Vertex-Articulation-point
func ArticulationPoint(graph *Graph) []bool {
// Time variable to keep track of the discovery time of a vertex
time := 0
// Initialize apHelper instance with the required data structures
apHelperInstance := &apHelper{
isAP: make([]bool, graph.vertices),
visited: make([]bool, graph.vertices),
childCount: make([]int, graph.vertices),
discoveryTime: make([]int, graph.vertices),
earliestDiscovery: make([]int, graph.vertices),
}
// Start traversal from the root (0)
articulationPointHelper(apHelperInstance, 0, -1, &time, graph)
// Check if the root has only one child, making it non-articulate
if apHelperInstance.childCount[0] == 1 {
apHelperInstance.isAP[0] = false
}
return apHelperInstance.isAP
}
// articulationPointHelper recursively traverses the graph using DFS and marks articulation points.
// It updates `childCount`, `discoveryTime`, and `earliestDiscovery` slices for the given vertex.
func articulationPointHelper(
apHelperInstance *apHelper,
vertex int,
parent int,
time *int,
graph *Graph,
) {
apHelperInstance.visited[vertex] = true
// Set discovery and earliest discovery times for the vertex
apHelperInstance.discoveryTime[vertex] = *time
apHelperInstance.earliestDiscovery[vertex] = *time
*time++
for nextVertex := range graph.edges[vertex] {
if nextVertex == parent {
continue
}
if apHelperInstance.visited[nextVertex] {
// Update the earliest discovery time to the smallest reachable discovery time
apHelperInstance.earliestDiscovery[vertex] = min.Int(
apHelperInstance.earliestDiscovery[vertex],
apHelperInstance.discoveryTime[nextVertex],
)
continue
}
// Increment child count and perform recursive traversal for DFS
apHelperInstance.childCount[vertex]++
articulationPointHelper(apHelperInstance, nextVertex, vertex, time, graph)
// Update the earliest discovery time post DFS
apHelperInstance.earliestDiscovery[vertex] = min.Int(
apHelperInstance.earliestDiscovery[vertex],
apHelperInstance.earliestDiscovery[nextVertex],
)
// Mark vertex as articulation point if condition meets
if apHelperInstance.earliestDiscovery[nextVertex] >= apHelperInstance.discoveryTime[vertex] {
apHelperInstance.isAP[vertex] = true
}
}
}
================================================
FILE: graph/articulationpoints_test.go
================================================
package graph
import (
"reflect"
"testing"
)
func TestArticulationPoints(t *testing.T) {
var testCases = []struct {
description string
graph Graph
expected []bool
}{
{
"Linear tree structure",
Graph{
vertices: 5,
edges: map[int]map[int]int{
0: {
1: 0,
},
1: {
0: 0,
2: 0,
},
2: {
1: 0,
3: 0,
},
3: {
2: 0,
4: 0,
},
},
},
[]bool{false, true, true, true, false},
}, {
"A complete graph",
Graph{
vertices: 4,
edges: map[int]map[int]int{
0: {
1: 0,
2: 0,
3: 0,
},
1: {
0: 0,
2: 0,
3: 0,
},
2: {
0: 0,
1: 0,
3: 0,
},
3: {
0: 0,
1: 0,
2: 0,
},
},
},
[]bool{false, false, false, false},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
is_ap := ArticulationPoint(&test.graph)
if !reflect.DeepEqual(is_ap, test.expected) {
t.Logf("FAIL: %s", test.description)
}
})
}
}
================================================
FILE: graph/bellmanford.go
================================================
// The Bellman–Ford algorithm is an algorithm that computes shortest paths from a
// single source vertex to all of the other vertices in a weighted directed graph.
// It is slower than Dijkstra but capable of handling negative edge weights.
// https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
// Implementation is based on the book 'Introduction to Algorithms' (CLRS)
// time complexity: O(V*E) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
package graph
import (
"errors"
"math"
)
func (g *Graph) BellmanFord(start, end int) (isReachable bool, distance int, err error) {
INF := math.Inf(1)
distances := make([]float64, g.vertices)
// Set all vertices to unreachable, initialize source
for i := 0; i < g.vertices; i++ {
distances[i] = INF
}
distances[start] = 0
// Making iterations equal to #vertices
for n := 0; n < g.vertices; n++ {
// Looping over all edges
for u, adjacents := range g.edges {
for v, weightUV := range adjacents {
// If new shorter distance is found, update distance value (relaxation step)
if newDistance := distances[u] + float64(weightUV); distances[v] > newDistance {
distances[v] = newDistance
}
}
}
}
// Check for negative weight cycle
for u, adjacents := range g.edges {
for v, weightUV := range adjacents {
if newDistance := distances[u] + float64(weightUV); distances[v] > newDistance {
return false, -1, errors.New("negative weight cycle present")
}
}
}
return distances[end] != INF, int(distances[end]), nil
}
================================================
FILE: graph/bellmanford_test.go
================================================
package graph
import (
"errors"
"fmt"
"math"
"testing"
)
func TestBellmanford(t *testing.T) {
var testCases = []struct {
name string
edges [][]int
vertices int
start int
end int
isReachable bool
distance int
err error
}{
{
"single edge",
[][]int{
{0, 1, 1},
},
2, 0, 1, true, 1, nil,
},
{
"negative weights",
[][]int{
{0, 1, 1},
{1, 2, -3},
{2, 1, 4},
{2, 3, 1},
},
4, 0, 1, true, 1, nil,
},
{
"negative cycle",
[][]int{
{0, 1, 1},
{1, 2, -3},
{2, 1, 1},
{2, 3, 1},
},
4, 0, 1, false, -1, errors.New("negative weight cycle present"),
},
{
"unreachable vertex",
[][]int{
{0, 6, 771},
{0, 9, 782},
{1, 2, 454},
{2, 8, 48},
{3, 8, 249},
{3, 9, 880},
{3, 5, 280},
{7, 1, 92},
{7, 2, 497},
{8, 1, 102},
{8, 4, 977},
},
10, 8, 3, false, int(math.Inf(1)), nil,
},
{
"disconnected graph",
[][]int{
{0, 1, 10},
{2, 3, 15},
{3, 5, 10},
},
6, 0, 3, false, int(math.Inf(1)), nil,
},
{
"multiple paths",
[][]int{
{0, 1, 5},
{1, 2, 10},
{1, 3, 30},
{2, 4, 10},
{4, 5, 15},
{3, 5, 10},
},
6, 0, 5, true, 40, nil,
},
{
"random 1",
[][]int{
{0, 1, 10},
{1, 2, 10},
{0, 2, 100},
{2, 0, -10},
{1, 2, 1},
},
3, 0, 1, true, 10, nil,
},
{
"random 2",
[][]int{
{0, 1, 5498},
{2, 0, 7679},
{0, 3, 4999},
{1, 2, 8629},
{1, 3, -948},
{2, 3, 6231},
},
4, 0, 3, true, 4550, nil,
},
}
for _, test := range testCases {
t.Run(fmt.Sprint(test.name), func(t *testing.T) {
// Initializing graph, adding edges
graph := New(test.vertices)
graph.Directed = true
for _, edge := range test.edges {
graph.AddWeightedEdge(edge[0], edge[1], edge[2])
}
resIsReachable, resDistance, resError := graph.BellmanFord(test.start, test.end)
if resDistance != test.distance {
t.Errorf("Distance, Expected: %d, Computed: %d", test.distance, resDistance)
}
if resIsReachable != test.isReachable {
t.Errorf("Reachable, Expected: %t, Computed: %t", test.isReachable, resIsReachable)
}
if !errors.Is(test.err, resError) {
if resError == nil || test.err == nil {
t.Errorf("Reachable, Expected: %s, Computed: %s", test.err, resError)
} else if resError.Error() != test.err.Error() {
t.Errorf("Reachable, Expected: %s, Computed: %s", test.err.Error(), resError.Error())
}
}
})
}
}
================================================
FILE: graph/breadthfirstsearch.go
================================================
package graph
// BreadthFirstSearch is an algorithm for traversing and searching graph data structures.
// It starts at an arbitrary node of a graph, and explores all of the neighbor nodes
// at the present depth prior to moving on to the nodes at the next depth level.
// Worst-case performance O(|V|+|E|)=O(b^{d})}O(|V|+|E|)=O(b^{d}) where |V| is the number of vertices and |E| is the number of edges in the graph and b is the branching factor of the graph (the average number of successors of a node). d is the depth of the goal node.
// Worst-case space complexity O(|V|)=O(b^{d})}O(|V|)=O(b^{d}) where |V| is the number of vertices and |E| is the number of edges in the graph and b is the branching factor of the graph (the average number of successors of a node). d is the depth of the goal node.
// reference: https://en.wikipedia.org/wiki/Breadth-first_search
func BreadthFirstSearch(start, end, nodes int, edges [][]int) (isConnected bool, distance int) {
queue := make([]int, 0)
discovered := make([]int, nodes)
discovered[start] = 1
queue = append(queue, start)
for len(queue) > 0 {
v := queue[0]
queue = queue[1:]
for i := 0; i < len(edges[v]); i++ {
if discovered[i] == 0 && edges[v][i] > 0 {
if i == end {
return true, discovered[v]
}
discovered[i] = discovered[v] + 1
queue = append(queue, i)
}
}
}
return false, 0
}
================================================
FILE: graph/breadthfirstsearch_test.go
================================================
package graph
import (
"testing"
)
func TestBreadthFirstSearch(t *testing.T) {
var bfsTestData = []struct {
description string
start int
end int
nodes int
edges [][]int
expected1 bool
expected2 int
}{
{
"test 1 connected with distance 2",
0,
5,
6,
[][]int{
{0, 1, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 0},
{0, 1, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0},
},
true,
2,
},
{
"test 2 connected with distance 4",
0,
5,
6,
[][]int{
{0, 1, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0},
{0, 1, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 0},
},
true,
4,
},
{
"test 2 not connected",
0,
5,
6,
[][]int{
{0, 1, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0},
{0, 1, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0},
},
false,
0,
},
}
for _, test := range bfsTestData {
t.Run(test.description, func(t *testing.T) {
r1, r2 := BreadthFirstSearch(test.start, test.end,
test.nodes, test.edges)
if r1 != test.expected1 || r2 != test.expected2 {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Nodes '%v' and Edges '%v' start from '%d' and end in '%d' "+
"was expecting '%v' with distance '%d' but result was '%v','%d'",
test.nodes, test.edges, test.start, test.end, test.expected1, test.expected2, r1, r2)
}
})
}
}
================================================
FILE: graph/coloring/backtracking.go
================================================
// This file contains the graph coloring implementation using backtracking
// time complexity: O(V^V) where V is the number of vertices in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
// ColorUsingBacktracking will return the Color of each vertex and the
// total number of different colors used, using backtracking
func (g *Graph) ColorUsingBacktracking() (map[int]Color, int) {
vertexColors := make(map[int]Color, g.vertices)
g.colorVertex(0, vertexColors)
colorsUsed := 0
for _, cr := range vertexColors {
if colorsUsed < int(cr) {
colorsUsed = int(cr)
}
}
return vertexColors, colorsUsed
}
// colorVertex will try to color provided vertex, v
func (g *Graph) colorVertex(v int, color map[int]Color) bool {
// If all vertices are colored, the colors store will be completely filled.
if len(color) == g.vertices {
return true
}
// As the upper bound of no. of colors is the no. of vertices in graph,
// try assigning each color to the vertex v
for cr := Color(1); cr <= Color(g.vertices); cr++ {
// Use the color, cr for vertex, v if it is safe to use, by
// checking its neighbours
safe := true
for nb := range g.edges[v] {
// cr, color is not safe if color of nb, crnb is not equal to cr
if crnb, ok := color[nb]; ok && crnb == cr {
safe = false
break
}
}
if safe {
color[v] = cr
if g.colorVertex(v+1, color) {
return true
}
delete(color, v)
}
}
return false
}
================================================
FILE: graph/coloring/backtracking_test.go
================================================
// This file provides tests for coloring using backtracking.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring_test
import (
"strconv"
"testing"
)
func TestGraphColorUsingBacktracking(t *testing.T) {
for i, tt := range getTestGraphs() {
t.Run(strconv.Itoa(i), func(t *testing.T) {
colorsOfVertices, colors := tt.Graph.ColorUsingBacktracking()
if colors != tt.ColorsUsed {
t.Errorf("ColorUsingBacktracking() return more number of colors: %v, want %v colors", colors, tt.ColorsUsed)
}
// check colors
if err := tt.Graph.ValidateColorsOfVertex(colorsOfVertices); err != nil {
t.Errorf("ColorUsingBacktracking() assigned colors are wrong, error = %v", err)
}
})
}
}
================================================
FILE: graph/coloring/bfs.go
================================================
// This file contains the graph coloring implementation using BFS
// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
import "container/list"
// ColorUsingBFS will return the Color of each vertex and the
// total number of different colors used, using BFS
func (g *Graph) ColorUsingBFS() (map[int]Color, int) {
// Initially all vertices will have same color
vertexColors := make(map[int]Color, g.vertices)
for i := 0; i < g.vertices; i++ {
vertexColors[i] = 1
}
visited := make(map[int]struct{})
// Run BFS from each non-visited vertex
for i := 0; i < g.vertices; i++ {
if _, ok := visited[i]; ok {
continue
}
visited[i] = struct{}{}
queue := list.New()
queue.PushBack(i)
for queue.Len() != 0 {
// front vertex in the queue
frontNode := queue.Front()
front := frontNode.Value.(int)
queue.Remove(frontNode)
// Now, check all neighbours of front vertex, if they have same
// color as that of front, change their color
for nb := range g.edges[front] {
if vertexColors[nb] == vertexColors[front] {
vertexColors[nb]++
}
// if the neighbour is not already visited, add it to the queue
if _, ok := visited[nb]; !ok {
visited[nb] = struct{}{}
queue.PushBack(nb)
}
}
}
}
colorsUsed := 0
for _, cr := range vertexColors {
if colorsUsed < int(cr) {
colorsUsed = int(cr)
}
}
return vertexColors, colorsUsed
}
================================================
FILE: graph/coloring/bfs_test.go
================================================
// This file provides tests for coloring using BFS.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring_test
import (
"strconv"
"testing"
)
func TestGraphColorUsingBFS(t *testing.T) {
for i, tt := range getTestGraphs() {
t.Run(strconv.Itoa(i), func(t *testing.T) {
colorsOfVertices, colors := tt.Graph.ColorUsingBFS()
if colors != tt.ColorsUsed {
t.Errorf("ColorUsingBFS() return more number of colors: %v, want %v colors", colors, tt.ColorsUsed)
}
// check colors
if err := tt.Graph.ValidateColorsOfVertex(colorsOfVertices); err != nil {
t.Errorf("ColorUsingBFS() assigned colors are wrong, error = %v", err)
}
})
}
}
================================================
FILE: graph/coloring/bipartite.go
================================================
package coloring
// Bipartite.go
// description: Implementation of the Bipartite graph coloring algorithm
// details: A bipartite graph is a graph whose vertices can be divided into two disjoint sets U and V such that every edge connects a vertex in U to one in V. The Bipartite graph coloring algorithm is used to determine if a graph is bipartite or not.
// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
func (g *Graph) TryBipartiteColoring() map[int]Color {
// 0 is uncolored, 1/2 are colors
colors := make(map[int]Color)
visited := make(map[int]bool)
for i := range g.edges {
colors[i] = 0
visited[i] = false
}
var colorNode func(int)
colorNode = func(s int) {
visited[s] = true
coloring := []Color{0, 2, 1}
for n := range g.edges[s] {
if colors[n] == 0 {
colors[n] = coloring[colors[s]]
}
if !visited[n] {
colorNode(n)
}
}
}
for i := range g.edges {
if colors[i] == 0 {
colors[i] = 1
colorNode(i)
}
}
return colors
}
// basically tries to color the graph in two colors if each edge
// connects 2 differently colored nodes the graph can be considered bipartite
func BipartiteCheck(N int, edges [][]int) bool {
var graph Graph
for i := 0; i < N; i++ {
graph.AddVertex(i)
}
for _, e := range edges {
graph.AddEdge(e[0], e[1])
}
return graph.ValidateColorsOfVertex(graph.TryBipartiteColoring()) == nil
}
================================================
FILE: graph/coloring/bipartite_test.go
================================================
package coloring
import (
"testing"
)
var testCases = []struct {
name string
N int
isBipartite bool
edges [][]int
}{
{
"basic true", 2, true,
[][]int{{1, 0}},
},
{
"basic false", 3, false,
[][]int{{0, 1}, {1, 2}, {2, 0}},
},
}
func TestBipartite(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := BipartiteCheck(tc.N, tc.edges)
if tc.isBipartite != actual {
t.Errorf("failed %s: %v", tc.name, tc.edges)
}
})
}
}
================================================
FILE: graph/coloring/doc.go
================================================
// Package coloring provides implementation of different graph coloring
// algorithms, e.g. coloring using BFS, using Backtracking, using greedy
// approach.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
================================================
FILE: graph/coloring/graph.go
================================================
// This file contains the simple structural implementation of undirected
// graph, used in coloring algorithms.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
import "errors"
// Color provides a type for vertex color
type Color int
// Graph provides a structure to store an undirected graph.
// It is safe to use its empty object.
type Graph struct {
vertices int
edges map[int]map[int]struct{}
}
// AddVertex will add a new vertex in the graph, if the vertex already
// exist it will do nothing
func (g *Graph) AddVertex(v int) {
if g.edges == nil {
g.edges = make(map[int]map[int]struct{})
}
// Check if vertex is present or not
if _, ok := g.edges[v]; !ok {
g.vertices++
g.edges[v] = make(map[int]struct{})
}
}
// AddEdge will add a new edge between the provided vertices in the graph
func (g *Graph) AddEdge(one, two int) {
// Add vertices: one and two to the graph if they are not present
g.AddVertex(one)
g.AddVertex(two)
// and finally add the edges: one->two and two->one for undirected graph
g.edges[one][two] = struct{}{}
g.edges[two][one] = struct{}{}
}
func (g *Graph) ValidateColorsOfVertex(colors map[int]Color) error {
if g.vertices != len(colors) {
return errors.New("coloring: not all vertices of graph are colored")
}
// check colors
for vertex, neighbours := range g.edges {
for nb := range neighbours {
if colors[vertex] == colors[nb] {
return errors.New("coloring: same colors of neighbouring vertex")
}
}
}
return nil
}
================================================
FILE: graph/coloring/graph_test.go
================================================
// This file provides tests for graph coloring validations.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring_test
import (
"github.com/TheAlgorithms/Go/graph/coloring"
"strconv"
"testing"
)
type testGraph struct {
Graph *coloring.Graph
ColorsUsed int
VertexColors map[int]coloring.Color
}
func getTestGraphs() (list []*testGraph) {
// Graph 0th:
// 1---2
// | / \
// 4---3 0
// Min number of colors required = 3
g0 := &testGraph{
Graph: &coloring.Graph{},
ColorsUsed: 3,
VertexColors: map[int]coloring.Color{
1: 1, 4: 1, 0: 1,
2: 2,
3: 3,
},
}
list = append(list, g0)
g0.Graph.AddEdge(4, 3)
g0.Graph.AddEdge(3, 1)
g0.Graph.AddEdge(3, 2)
g0.Graph.AddEdge(1, 2)
g0.Graph.AddEdge(2, 0)
// Graph 1st:
// 1---2
// | / |
// 4---3---0
// Min number of colors required = 3
g1 := &testGraph{
Graph: &coloring.Graph{},
ColorsUsed: 3,
VertexColors: map[int]coloring.Color{
1: 1, 4: 1, 0: 1,
2: 2,
3: 3,
},
}
list = append(list, g1)
g1.Graph.AddEdge(4, 3)
g1.Graph.AddEdge(3, 1)
g1.Graph.AddEdge(3, 2)
g1.Graph.AddEdge(1, 2)
g1.Graph.AddEdge(2, 0)
g1.Graph.AddEdge(3, 0)
// Graph 2nd:
// 1---2
// |
// 4---3 0
// Min number of colors required = 2
g2 := &testGraph{
Graph: &coloring.Graph{},
ColorsUsed: 2,
VertexColors: map[int]coloring.Color{
1: 1, 4: 1, 0: 1,
2: 2, 3: 2,
},
}
list = append(list, g2)
g2.Graph.AddVertex(0)
g2.Graph.AddEdge(4, 3)
g2.Graph.AddEdge(3, 1)
g2.Graph.AddEdge(1, 2)
// Graph 3rd:
// 1---2 4
// | | |
// 0---3 5
// Min number of colors required = 2
g3 := &testGraph{
Graph: &coloring.Graph{},
ColorsUsed: 2,
VertexColors: map[int]coloring.Color{
1: 1, 3: 1, 4: 1,
0: 2, 2: 2, 5: 2,
},
}
list = append(list, g3)
g3.Graph.AddEdge(0, 3)
g3.Graph.AddEdge(1, 2)
g3.Graph.AddEdge(1, 0)
g3.Graph.AddEdge(3, 2)
g3.Graph.AddEdge(4, 5)
// Graph 4th:
// Completely Connected graph of vertex 4
// Min number of colors required = 2
g4 := &testGraph{
Graph: &coloring.Graph{},
ColorsUsed: 4,
VertexColors: map[int]coloring.Color{
0: 1, 1: 2, 2: 3, 4: 4,
},
}
list = append(list, g4)
for i := 0; i < 4; i++ {
for j := i + 1; j < 4; j++ {
g4.Graph.AddEdge(i, j)
}
}
return
}
func TestGraph_ValidateColorsOfVertex(t *testing.T) {
for i, tt := range getTestGraphs() {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if err := tt.Graph.ValidateColorsOfVertex(tt.VertexColors); err != nil {
t.Errorf("ValidateColorsOfVertex() error = %v, wantErr nil", err)
}
})
}
}
func getTestGraphsForNegativeTests() (list []*testGraph) {
list = getTestGraphs()
list[0].VertexColors = nil
list[1].VertexColors = map[int]coloring.Color{}
for v := range list[2].VertexColors {
in := len(list[2].VertexColors) - v - 1
list[2].VertexColors[v] = list[2].VertexColors[in]
}
for v := range list[3].VertexColors {
list[3].VertexColors[v] = 1
}
return list[:4]
}
func TestGraphValidateColorsOfVertex_Negative(t *testing.T) {
for i, tt := range getTestGraphsForNegativeTests() {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if err := tt.Graph.ValidateColorsOfVertex(tt.VertexColors); err == nil {
t.Errorf("ValidateColorsOfVertex() error = nil, want some err")
}
})
}
}
================================================
FILE: graph/coloring/greedy.go
================================================
// This file contains the graph coloring implementation using Greedy Approach.
// time complexity: O(V^2) where V is the number of vertices in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring
import "sort"
// ColorUsingGreedyApproach will return the Color of each vertex and the
// total number of different colors used, using a greedy approach, based on
// the number of edges (or degree) from any vertex.
func (g *Graph) ColorUsingGreedyApproach() (map[int]Color, int) {
degreeOfVertex := make([]struct{ degree, vertex int }, 0, g.vertices)
for v, neighbours := range g.edges {
degreeOfVertex = append(degreeOfVertex,
struct{ degree, vertex int }{
vertex: v,
degree: len(neighbours),
},
)
}
// sort the degreeOfVertex in decreasing order of degrees
sort.Slice(degreeOfVertex, func(i, j int) bool {
return degreeOfVertex[i].degree > degreeOfVertex[j].degree
})
vertexColors := make(map[int]Color, g.vertices)
// Start with a color and assign the color to all possible vertices in the degreeOfVertex slice
// and then, re-iterate with new color for all those which are left
for color := 1; color <= g.vertices; color++ {
vertexLoop:
for _, val := range degreeOfVertex {
// skip, if already assigned
if _, ok := vertexColors[val.vertex]; ok {
continue vertexLoop
}
// Check its neighbours
for ng := range g.edges[val.vertex] {
if vertexColors[ng] == Color(color) {
// not possible to use this color for val.vertex
continue vertexLoop
}
}
// Assign color to the vertex
vertexColors[val.vertex] = Color(color)
}
// continue till all the vertices are colored
if len(vertexColors) == g.vertices {
return vertexColors, color
}
}
return vertexColors, g.vertices
}
================================================
FILE: graph/coloring/greedy_test.go
================================================
// This file provides tests for coloring using Greedy approach.
// Author(s): [Shivam](https://github.com/Shivam010)
package coloring_test
import (
"strconv"
"testing"
)
func TestGraphColorUsingGreedyApproach(t *testing.T) {
for i, tt := range getTestGraphs() {
t.Run(strconv.Itoa(i), func(t *testing.T) {
colorsOfVertices, colors := tt.Graph.ColorUsingGreedyApproach()
if colors != tt.ColorsUsed {
t.Errorf("ColorUsingGreedyApproach() return more number of colors: %v, want %v colors", colors, tt.ColorsUsed)
}
// check colors
if err := tt.Graph.ValidateColorsOfVertex(colorsOfVertices); err != nil {
t.Errorf("ColorUsingGreedyApproach() assigned colors are wrong, error = %v", err)
}
})
}
}
================================================
FILE: graph/cycle.go
================================================
// cycle.go
// this file handle algorithm that related to cycle in graph
// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// reference: https://en.wikipedia.org/wiki/Cycle_(graph_theory)
// [kiarash hajian](https://github.com/kiarash8112)
package graph
func (g *Graph) HasCycle() bool {
//this implimetation referred as 3-color too
all := map[int]struct{}{}
visiting := map[int]struct{}{}
visited := map[int]struct{}{}
for v := range g.edges {
all[v] = struct{}{}
}
for current := range all {
if g.hasCycleHelper(current, all, visiting, visited) {
return true
}
}
return false
}
func (g Graph) hasCycleHelper(v int, all, visiting, visited map[int]struct{}) bool {
delete(all, v)
visiting[v] = struct{}{}
neighbors := g.edges[v]
for v := range neighbors {
if _, ok := visited[v]; ok {
continue
} else if _, ok := visiting[v]; ok {
return true
} else if g.hasCycleHelper(v, all, visiting, visited) {
return true
}
}
delete(visiting, v)
visited[v] = struct{}{}
return false
}
// this function can do HasCycle() job but it is slower
func (g *Graph) FindAllCycles() []Graph {
all := map[int]struct{}{}
visiting := map[int]struct{}{}
visited := map[int]struct{}{}
allCycles := []Graph{}
for v := range g.edges {
all[v] = struct{}{}
}
for current := range all {
foundCycle, parents := g.findAllCyclesHelper(current, all, visiting, visited)
if foundCycle {
foundCycleFromCurrent := false
//this loop remove additional vertex from detected cycle
//using foundCycleFromCurrent bool to make sure after removing vertex we still have cycle
for i := len(parents) - 1; i > 0; i-- {
if parents[i][1] == parents[0][0] {
parents = parents[:i+1]
foundCycleFromCurrent = true
}
}
if foundCycleFromCurrent {
graph := Graph{Directed: true}
for _, edges := range parents {
graph.AddEdge(edges[1], edges[0])
}
allCycles = append(allCycles, graph)
}
}
}
return allCycles
}
func (g Graph) findAllCyclesHelper(current int, all, visiting, visited map[int]struct{}) (bool, [][]int) {
parents := [][]int{}
delete(all, current)
visiting[current] = struct{}{}
neighbors := g.edges[current]
for v := range neighbors {
if _, ok := visited[v]; ok {
continue
} else if _, ok := visiting[v]; ok {
parents = append(parents, []int{v, current})
return true, parents
} else if ok, savedParents := g.findAllCyclesHelper(v, all, visiting, visited); ok {
parents = append(parents, savedParents...)
parents = append(parents, []int{v, current})
return true, parents
}
}
delete(visiting, current)
visited[current] = struct{}{}
return false, parents
}
================================================
FILE: graph/cycle_test.go
================================================
package graph
import (
"testing"
)
func TestHasCycle(t *testing.T) {
graph := Graph{Directed: true}
edges := [][]int{{0, 1}, {1, 2}, {2, 0}, {4, 0}}
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
if !graph.HasCycle() {
t.Error("answer of hasCycle is not correct")
}
graph = Graph{Directed: true}
edges = [][]int{{0, 1}, {1, 2}, {2, 6}, {4, 0}}
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
if graph.HasCycle() {
t.Error("answer of hasCycle is not correct")
}
}
func TestFindAllCycles(t *testing.T) {
graph := Graph{Directed: true}
edges := [][]int{{0, 4}, {1, 3}, {2, 3}, {3, 4}, {4, 7}, {5, 2}, {6, 3}, {7, 3}}
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
res := graph.FindAllCycles()
if len(res) != 1 {
t.Error("number of cycles is not correct")
}
firstCycle := res[0]
if len(firstCycle.edges) != 3 {
t.Error("number of vertex in cycle is not correct")
}
if _, ok := firstCycle.edges[3][4]; !ok {
t.Error("connection in cycle is not correct")
}
if _, ok := firstCycle.edges[4][7]; !ok {
t.Error("connection in cycle is not correct")
}
if _, ok := firstCycle.edges[7][3]; !ok {
t.Error("connection in cycle is not correct")
}
}
================================================
FILE: graph/depthfirstsearch.go
================================================
// depthfirstsearch.go
// description: this file contains the implementation of the depth first search algorithm
// details: Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node and explores as far as possible along each branch before backtracking.
// time complexity: O(n)
// space complexity: O(n)
package graph
func GetIdx(target int, nodes []int) int {
for i := 0; i < len(nodes); i++ {
if nodes[i] == target {
return i
}
}
return -1
}
func NotExist(target int, slice []int) bool {
for i := 0; i < len(slice); i++ {
if slice[i] == target {
return false
}
}
return true
}
func DepthFirstSearchHelper(start, end int, nodes []int, edges [][]bool, showroute bool) ([]int, bool) {
var route []int
var stack []int
startIdx := GetIdx(start, nodes)
stack = append(stack, startIdx)
for len(stack) > 0 {
now := stack[len(stack)-1]
route = append(route, nodes[now])
if len(stack) > 1 {
stack = stack[:len(stack)-1]
} else {
stack = stack[:len(stack)-1]
}
for i := 0; i < len(edges[now]); i++ {
if edges[now][i] && NotExist(i, stack) {
stack = append(stack, i)
}
edges[now][i] = false
edges[i][now] = false
}
if route[len(route)-1] == end {
return route, true
}
}
if showroute {
return route, false
} else {
return nil, false
}
}
func DepthFirstSearch(start, end int, nodes []int, edges [][]bool) ([]int, bool) {
return DepthFirstSearchHelper(start, end, nodes, edges, false)
}
// func main() {
// nodes := []int{
// 1, 2, 3, 4, 5, 6,
// }
// /*
// sample graph
// ①-②
// | |
// ③-④-⑤-⑥
// */
// edges := [][]bool{
// {false, true, true, false, false, false},
// {true, false, false, true, false, false},
// {true, false, false, true, false, false},
// {false, true, true, false, true, false},
// {false, false, false, true, false, true},
// {false, false, false, false, true, false},
// }
// start := 1
// end := 6
// route, _ := dfs(start, end, nodes, edges)
// fmt.Println(route)
// }
================================================
FILE: graph/depthfirstsearch_test.go
================================================
package graph
import (
"reflect"
"testing"
)
func TestDfsWhenPathIsFound(t *testing.T) {
nodes := []int{
1, 2, 3, 4, 5, 6,
}
//Adjacency Matrix for connected nodes
edges := [][]bool{
{false, true, true, false, false, false},
{true, false, false, true, false, false},
{true, false, false, true, false, false},
{false, true, true, false, true, false},
{false, false, false, true, false, true},
{false, false, false, false, true, false},
}
start := 1
end := 6
actual, actualIsFound := DepthFirstSearch(start, end, nodes, edges)
expected := []int{1, 3, 4, 5, 6}
expectedIsFound := true
t.Run("Test Dfs", func(t *testing.T) {
if !reflect.DeepEqual(expected, actual) || !reflect.DeepEqual(actualIsFound, expectedIsFound) {
t.Errorf("got route: %v, want route: %v", actual, expected)
t.Errorf("got isFound: %v, want isFound: %v", actualIsFound, expectedIsFound)
}
})
}
func TestDfsWhenPathIsNotFound(t *testing.T) {
nodes := []int{
1, 2, 3, 4, 5, 6,
}
//Adjacency Matrix for connected nodes
edges := [][]bool{
{false, true, true, false, false, false},
{true, false, false, true, false, false},
{true, false, false, true, false, false},
{false, true, true, false, true, false},
{false, false, false, true, false, true},
{false, false, false, false, true, false},
}
start := 1
end := 7
actual, actualIsFound := DepthFirstSearch(start, end, nodes, edges)
var expected []int
expectedIsFound := false
t.Run("Test Dfs", func(t *testing.T) {
if !reflect.DeepEqual(expected, actual) || !reflect.DeepEqual(actualIsFound, expectedIsFound) {
t.Errorf("got route: %v, want route: %v", actual, expected)
t.Errorf("got isFound: %v, want isFound: %v", actualIsFound, expectedIsFound)
}
})
}
================================================
FILE: graph/dijkstra.go
================================================
// dijkstra.go
// description: this file contains the implementation of the Dijkstra algorithm
// details: Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later. The algorithm exists in many variants; Dijkstra's original variant found the shortest path between two nodes, but a more common variant fixes a single node as the "source" node and finds shortest paths from the source to all other nodes in the graph, producing a shortest-path tree.
// time complexity: O((V+E) log V) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// reference: https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
package graph
import "github.com/TheAlgorithms/Go/sort"
type Item struct {
node int
dist int
}
func (a Item) More(b any) bool {
// reverse direction for minheap
return a.dist < b.(Item).dist
}
func (a Item) Idx() int {
return a.node
}
func (g *Graph) Dijkstra(start, end int) (int, bool) {
visited := make(map[int]bool)
nodes := make(map[int]*Item)
nodes[start] = &Item{
dist: 0,
node: start,
}
pq := sort.MaxHeap{}
pq.Init(nil)
pq.Push(*nodes[start])
visit := func(curr Item) {
visited[curr.node] = true
for n, d := range g.edges[curr.node] {
if visited[n] {
continue
}
item := nodes[n]
dist2 := curr.dist + d
if item == nil {
nodes[n] = &Item{node: n, dist: dist2}
pq.Push(*nodes[n])
} else if item.dist > dist2 {
item.dist = dist2
pq.Update(*item)
}
}
}
for pq.Size() > 0 {
curr := pq.Pop().(Item)
if curr.node == end {
break
}
visit(curr)
}
item := nodes[end]
if item == nil {
return -1, false
}
return item.dist, true
}
================================================
FILE: graph/dijkstra_test.go
================================================
package graph
import (
"testing"
)
var tc_dijkstra = []struct {
name string
edges [][]int
node0 int
node1 int
expected int
}{
{
"straight line graph",
[][]int{{0, 1, 5}, {1, 2, 2}},
0, 2, 7,
},
{
"unconnected node",
[][]int{{0, 1, 5}},
0, 2, -1,
},
{
"double paths",
[][]int{{0, 1, 5}, {1, 3, 5}, {0, 2, 5}, {2, 3, 4}},
0, 3, 9,
},
{
"double paths extended",
[][]int{{0, 1, 5}, {1, 3, 5}, {0, 2, 5}, {2, 3, 4}, {3, 4, 1}},
0, 4, 10,
},
}
func TestDijkstra(t *testing.T) {
for _, tc := range tc_dijkstra {
t.Run(tc.name, func(t *testing.T) {
var graph Graph
for _, edge := range tc.edges {
graph.AddWeightedEdge(edge[0], edge[1], edge[2])
}
actual, _ := graph.Dijkstra(tc.node0, tc.node1)
if actual != tc.expected {
t.Errorf("expected %d, got %d, from node %d to %d, with %v",
tc.expected, actual, tc.node0, tc.node1, tc.edges)
}
})
}
}
================================================
FILE: graph/doc.go
================================================
// Package graph demonstrates Graph search algorithms
// reference: https://en.wikipedia.org/wiki/Tree_traversal
package graph
================================================
FILE: graph/edmondkarp.go
================================================
// Edmond-Karp algorithm is an implementation of the Ford-Fulkerson method
// to compute max-flow between a pair of source-sink vertices in a weighted graph
// It uses BFS (Breadth First Search) to find the residual paths
// Time Complexity: O(V * E^2) where V is the number of vertices and E is the number of edges
// Space Complexity: O(V + E) Because we keep residual graph in size of the original graph
// Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, Third Edition (3rd. ed.). The MIT Press.
package graph
import (
"math"
)
// Returns a mapping of vertices as path, if there is any from source to sink
// Otherwise, returns nil
func FindPath(rGraph WeightedGraph, source int, sink int) map[int]int {
queue := make([]int, 0)
marked := make([]bool, len(rGraph))
marked[source] = true
queue = append(queue, source)
parent := make(map[int]int)
// BFS loop with saving the path found
for len(queue) > 0 {
v := queue[0]
queue = queue[1:]
for i := 0; i < len(rGraph[v]); i++ {
if !marked[i] && rGraph[v][i] > 0 {
parent[i] = v
// Terminate the BFS, if we reach to sink
if i == sink {
return parent
}
marked[i] = true
queue = append(queue, i)
}
}
}
// source and sink are not in the same connected component
return nil
}
func EdmondKarp(graph WeightedGraph, source int, sink int) float64 {
// Check graph emptiness
if len(graph) == 0 {
return 0.0
}
// Check correct dimensions of the graph slice
for i := 0; i < len(graph); i++ {
if len(graph[i]) != len(graph) {
return 0.0
}
}
rGraph := make(WeightedGraph, len(graph))
for i := 0; i < len(graph); i++ {
rGraph[i] = make([]float64, len(graph))
}
// Init the residual graph with the same capacities as the original graph
copy(rGraph, graph)
maxFlow := 0.0
for {
parent := FindPath(rGraph, source, sink)
if parent == nil {
break
}
// Finding the max flow over the path returned by BFS
// i.e. finding minimum residual capacity amonth the path edges
pathFlow := math.MaxFloat64
for v := sink; v != source; v = parent[v] {
u := parent[v]
if rGraph[u][v] < pathFlow {
pathFlow = rGraph[u][v]
}
}
// update residual capacities of the edges and
// reverse edges along the path
for v := sink; v != source; v = parent[v] {
u := parent[v]
rGraph[u][v] -= pathFlow
rGraph[v][u] += pathFlow
}
// Update the total flow found so far
maxFlow += pathFlow
}
return maxFlow
}
================================================
FILE: graph/edmondkarp_test.go
================================================
package graph
import (
"testing"
)
func TestEdmondKarp(t *testing.T) {
var edmondKarpTestData = []struct {
description string
graph WeightedGraph
source int
sink int
expected float64
}{
{
description: "test empty graph",
graph: nil,
source: 0,
sink: 0,
expected: 0.0,
},
{
description: "test graph with wrong dimensions",
graph: WeightedGraph{
{1, 2},
{0},
},
source: 0,
sink: 1,
expected: 0.0,
},
{
description: "test graph with no edges",
graph: WeightedGraph{
{0, 0},
{0, 0},
},
source: 0,
sink: 1,
expected: 0.0,
},
{
description: "test graph with 4 vertices",
graph: WeightedGraph{
{0, 1000000, 1000000, 0},
{0, 0, 1, 1000000},
{0, 0, 0, 1000000},
{0, 0, 0, 0},
},
source: 0,
sink: 3,
expected: 2000000,
},
{
description: "test graph with 6 vertices and some float64 weights",
graph: WeightedGraph{
{0, 16, 13.8, 0, 0, 0},
{0, 0, 10, 12.7, 0, 0},
{0, 4.2, 0, 0, 14, 0},
{0, 0, 9.1, 0, 0, 21.3},
{0, 0, 0, 7.5, 0, 4},
{0, 0, 0, 0, 0, 0},
},
source: 0,
sink: 5,
expected: 24.2,
},
}
for _, test := range edmondKarpTestData {
t.Run(test.description, func(t *testing.T) {
result := EdmondKarp(test.graph, test.source, test.sink)
if !almostEqual(test.expected, result) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expected result:%f\nFound: %f", test.expected, result)
}
})
}
}
================================================
FILE: graph/floydwarshall.go
================================================
// Floyd-Warshall algorithm
// time complexity: O(V^3) where V is the number of vertices in the graph
// space complexity: O(V^2) where V is the number of vertices in the graph
// https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
package graph
import "math"
// WeightedGraph defining matrix to use 2d array easier
type WeightedGraph [][]float64
// Defining maximum value. If two vertices share this value, it means they are not connected
var Inf = math.Inf(1)
// FloydWarshall Returns all pair's shortest path using Floyd Warshall algorithm
func FloydWarshall(graph WeightedGraph) WeightedGraph {
// If graph is empty, returns nil
if len(graph) == 0 || len(graph) != len(graph[0]) {
return nil
}
for i := 0; i < len(graph); i++ {
//If graph matrix width is different than the height, returns nil
if len(graph[i]) != len(graph) {
return nil
}
}
numVertices := len(graph)
// Initializing result matrix and filling it up with same values as given graph
result := make(WeightedGraph, numVertices)
for i := 0; i < numVertices; i++ {
result[i] = make([]float64, numVertices)
for j := 0; j < numVertices; j++ {
result[i][j] = graph[i][j]
}
}
// Running over the result matrix and following the algorithm
for k := 0; k < numVertices; k++ {
for i := 0; i < numVertices; i++ {
for j := 0; j < numVertices; j++ {
// If there is a less costly path from i to j node, remembering it
if result[i][j] > result[i][k]+result[k][j] {
result[i][j] = result[i][k] + result[k][j]
}
}
}
}
return result
}
================================================
FILE: graph/floydwarshall_test.go
================================================
package graph
import (
"math"
"testing"
)
const float64EqualityThreshold = 1e-9
// almostEqual subtracts two float64 variables and returns true if they differ less then float64EqualityThreshold
// reference: https://stackoverflow.com/a/47969546
func almostEqual(a, b float64) bool {
return math.Abs(a-b) <= float64EqualityThreshold
}
// IsAlmostEqualTo verifies if two WeightedGraphs can be considered almost equal
func (a *WeightedGraph) IsAlmostEqualTo(b WeightedGraph) bool {
if len(*a) != len(b) {
return false
}
for i := range *a {
if len((*a)[i]) != len(b[i]) {
return false
}
for j := range (*a)[i] {
if (*a)[i][j] == Inf && b[i][j] == Inf {
continue
}
if !almostEqual((*a)[i][j], b[i][j]) {
return false
}
}
}
return true
}
func TestFloydWarshall(t *testing.T) {
var floydWarshallTestData = []struct {
description string
graph [][]float64
expected [][]float64
}{
{
description: "test empty graph",
graph: nil,
expected: nil,
},
{
description: "test graph with wrong dimensions",
graph: [][]float64{
{1, 2},
{Inf},
},
expected: nil,
},
{
description: "test graph with no edges",
graph: [][]float64{
{Inf, Inf},
{Inf, Inf},
},
expected: [][]float64{
{Inf, Inf},
{Inf, Inf},
},
},
{
description: "test graph with only negative edges",
graph: [][]float64{
{-3, -2},
{-3, -2},
},
expected: [][]float64{
{-17, -25},
{-26, -34},
},
},
{
description: "test graph with 5 vertices and self-loops",
graph: [][]float64{
{1, 2, Inf, Inf, Inf},
{Inf, Inf, 3, -4, Inf},
{Inf, Inf, Inf, Inf, 5},
{1, Inf, Inf, Inf, Inf},
{Inf, Inf, Inf, 2, Inf},
},
expected: [][]float64{
{-1, 1, 4, -3, 8},
{-3, -1, 2, -5, 6},
{7, 9, 12, 5, 5},
{0, 2, 5, -2, 9},
{2, 4, 7, 0, 9},
},
},
}
for _, test := range floydWarshallTestData {
t.Run(test.description, func(t *testing.T) {
result := FloydWarshall(test.graph)
if !result.IsAlmostEqualTo(test.expected) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expected result:%f\nFound: %f", test.expected, result)
}
})
}
}
================================================
FILE: graph/graph.go
================================================
// This file contains the simple structural implementation of
// directed & undirected graphs used within the graph package
// Author(s): [Shivam](https://github.com/Shivam010), [Tahmeed](https://github.com/Tahmeed156)
package graph
// Graph provides a structure to store the graph.
// It is safe to use its empty object.
type Graph struct {
vertices int
edges map[int]map[int]int // Stores weight of an edge
Directed bool // Differentiate directed/undirected graphs
}
// Constructor functions for graphs (undirected by default)
func New(v int) *Graph {
return &Graph{
vertices: v,
}
}
// AddVertex will add a new vertex in the graph.
// If the vertex already exists it will do nothing.
func (g *Graph) AddVertex(v int) {
if g.edges == nil {
g.edges = make(map[int]map[int]int)
}
// Check if vertex is present or not
if _, ok := g.edges[v]; !ok {
g.edges[v] = make(map[int]int)
}
}
// AddEdge will add a new edge between the provided vertices in the graph
func (g *Graph) AddEdge(one, two int) {
// Add an edge with 0 weight
g.AddWeightedEdge(one, two, 0)
}
// AddWeightedEdge will add a new weighted edge between the provided vertices in the graph
func (g *Graph) AddWeightedEdge(one, two, weight int) {
// Add vertices: one and two to the graph if they are not present
g.AddVertex(one)
g.AddVertex(two)
// And finally add the edges
// one->two and two->one for undirected graph
// one->two for directed graphs
g.edges[one][two] = weight
if !g.Directed {
g.edges[two][one] = weight
}
}
================================================
FILE: graph/graph_test.go
================================================
// Tests for directed and undirected graphs
package graph
import (
"fmt"
"testing"
)
var graphTestCases = []struct {
name string
edges [][]int
vertices int
}{
{
"single edge",
[][]int{
{0, 1, 1},
},
2,
},
{
"many edges",
[][]int{
{0, 1, 1},
{0, 2, 2},
{1, 3, 4},
{3, 4, 3},
{4, 8, 3},
{4, 9, 1},
{7, 8, 2},
{8, 9, 2},
},
10,
},
{
"cycles",
[][]int{
{0, 1, 1},
{0, 2, 2},
{1, 3, 4},
{3, 4, 3},
{4, 2, 1},
},
5,
},
{
"disconnected graphs",
[][]int{
{0, 1, 5},
{2, 4, 5},
{3, 8, 5},
},
2,
},
}
func TestDirectedGraph(t *testing.T) {
// Testing self-loops separately only for directed graphs.
// For undirected graphs each edge already creates a self-loop.
directedGraphTestCases := append(graphTestCases, struct {
name string
edges [][]int
vertices int
}{
"self-loops",
[][]int{
{0, 1, 1},
{1, 2, 2},
{2, 1, 3},
},
3,
})
for _, test := range directedGraphTestCases {
t.Run(fmt.Sprint(test.name), func(t *testing.T) {
// Initializing graph, adding edges
graph := New(test.vertices)
graph.Directed = true
for _, edge := range test.edges {
graph.AddWeightedEdge(edge[0], edge[1], edge[2])
}
if graph.vertices != test.vertices {
t.Errorf("Number of vertices, Expected: %d, Computed: %d", test.vertices, graph.vertices)
}
edgeCount := 0
for _, e := range graph.edges {
edgeCount += len(e)
}
if edgeCount != len(test.edges) {
t.Errorf("Number of edges, Expected: %d, Computed: %d", len(test.edges), edgeCount)
}
for _, edge := range test.edges {
if val, found := graph.edges[edge[0]][edge[1]]; !found || val != edge[2] {
t.Errorf("Edge {%d->%d (%d)} not found", edge[0], edge[1], edge[2])
}
}
})
}
}
func TestUndirectedGraph(t *testing.T) {
for _, test := range graphTestCases {
t.Run(fmt.Sprint(test.name), func(t *testing.T) {
// Initializing graph, adding edges
graph := New(test.vertices)
for _, edge := range test.edges {
graph.AddWeightedEdge(edge[0], edge[1], edge[2])
}
if graph.vertices != test.vertices {
t.Errorf("Number of vertices, Expected: %d, Computed: %d", test.vertices, graph.vertices)
}
edgeCount := 0
for _, e := range graph.edges {
edgeCount += len(e)
}
if edgeCount != len(test.edges)*2 {
t.Errorf("Number of edges, Expected: %d, Computed: %d", len(test.edges)*2, edgeCount)
}
for _, edge := range test.edges {
if val, found := graph.edges[edge[0]][edge[1]]; !found || val != edge[2] {
t.Errorf("Edge {%d->%d (%d)} not found", edge[0], edge[1], edge[2])
}
}
})
}
}
================================================
FILE: graph/kahn.go
================================================
// Kahn's algorithm computes a topological ordering of a directed acyclic graph (DAG).
// Time Complexity: O(V + E)
// Space Complexity: O(V + E)
// Reference: https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
// see graph.go, topological.go, kahn_test.go
package graph
// Kahn's algorithm computes a topological ordering of a directed acyclic graph (DAG).
// `n` is the number of vertices,
// `dependencies` is a list of directed edges, where each pair [a, b] represents
// a directed edge from a to b (i.e. b depends on a).
// Vertices are assumed to be labelled 0, 1, ..., n-1.
// If the graph is not a DAG, the function returns nil.
func Kahn(n int, dependencies [][]int) []int {
g := Graph{vertices: n, Directed: true}
// track the in-degree (number of incoming edges) of each vertex
inDegree := make([]int, n)
// populate g with edges, increase the in-degree counts accordingly
for _, d := range dependencies {
// make sure we don't add the same edge twice
if _, ok := g.edges[d[0]][d[1]]; !ok {
g.AddEdge(d[0], d[1])
inDegree[d[1]]++
}
}
// queue holds all vertices with in-degree 0
// these vertices have no dependency and thus can be ordered first
queue := make([]int, 0, n)
for i := 0; i < n; i++ {
if inDegree[i] == 0 {
queue = append(queue, i)
}
}
// order holds a valid topological order
order := make([]int, 0, n)
// process the dependency-free vertices
// every time we process a vertex, we "remove" it from the graph
for len(queue) > 0 {
// pop the first vertex from the queue
vtx := queue[0]
queue = queue[1:]
// add the vertex to the topological order
order = append(order, vtx)
// "remove" all the edges coming out of this vertex
// every time we remove an edge, the corresponding in-degree reduces by 1
// if all dependencies on a vertex is removed, enqueue the vertex
for neighbour := range g.edges[vtx] {
inDegree[neighbour]--
if inDegree[neighbour] == 0 {
queue = append(queue, neighbour)
}
}
}
// if the graph is a DAG, order should contain all the certices
if len(order) != n {
return nil
}
return order
}
================================================
FILE: graph/kahn_test.go
================================================
package graph
import (
"testing"
)
func TestKahn(t *testing.T) {
testCases := []struct {
name string
n int
dependencies [][]int
wantNil bool
}{
{
"linear graph",
3,
[][]int{{0, 1}, {1, 2}},
false,
},
{
"diamond graph",
4,
[][]int{{0, 1}, {0, 2}, {1, 3}, {2, 3}},
false,
},
{
"star graph",
5,
[][]int{{0, 1}, {0, 2}, {0, 3}, {0, 4}},
false,
},
{
"disconnected graph",
5,
[][]int{{0, 1}, {0, 2}, {3, 4}},
false,
},
{
"cycle graph 1",
4,
[][]int{{0, 1}, {1, 2}, {2, 3}, {3, 0}},
true,
},
{
"cycle graph 2",
4,
[][]int{{0, 1}, {1, 2}, {2, 0}, {2, 3}},
true,
},
{
"single node graph",
1,
[][]int{},
false,
},
{
"empty graph",
0,
[][]int{},
false,
},
{
"redundant dependencies",
4,
[][]int{{0, 1}, {1, 2}, {1, 2}, {2, 3}},
false,
},
{
"island vertex",
4,
[][]int{{0, 1}, {0, 2}},
false,
},
{
"more complicated graph",
14,
[][]int{{1, 9}, {2, 0}, {3, 2}, {4, 5}, {4, 6}, {4, 7}, {6, 7},
{7, 8}, {9, 4}, {10, 0}, {10, 1}, {10, 12}, {11, 13},
{12, 0}, {12, 11}, {13, 5}},
false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := Kahn(tc.n, tc.dependencies)
if tc.wantNil {
if actual != nil {
t.Errorf("Kahn(%d, %v) = %v; want nil", tc.n, tc.dependencies, actual)
}
} else {
if actual == nil {
t.Errorf("Kahn(%d, %v) = nil; want valid order", tc.n, tc.dependencies)
} else {
seen := make([]bool, tc.n)
positions := make([]int, tc.n)
for i, v := range actual {
seen[v] = true
positions[v] = i
}
for i, v := range seen {
if !v {
t.Errorf("missing vertex %v", i)
}
}
for _, d := range tc.dependencies {
if positions[d[0]] > positions[d[1]] {
t.Errorf("dependency %v not satisfied", d)
}
}
}
}
})
}
}
================================================
FILE: graph/kosaraju.go
================================================
// kosaraju.go
// description: Implementation of Kosaraju's algorithm to find Strongly Connected Components (SCCs) in a directed graph.
// details: The algorithm consists of three steps:
// 1. Perform DFS and fill the stack with vertices in the order of their finish times.
// 2. Create a transposed graph by reversing all edges.
// 3. Perform DFS on the transposed graph in the order defined by the stack to find SCCs.
// time: O(V + E), where V is the number of vertices and E is the number of edges in the graph.
// space: O(V), where V is the number of vertices in the graph.
// ref link: https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
// author: mapcrafter2048
package graph
// Kosaraju returns a list of Strongly Connected Components (SCCs).
func (g *Graph) Kosaraju() [][]int {
stack := []int{}
visited := make([]bool, g.vertices)
// Step 1: Perform DFS and fill stack based on finish times.
for i := 0; i < g.vertices; i++ {
if !visited[i] {
g.fillOrder(i, visited, &stack)
}
}
// Step 2: Create a transposed graph.
transposed := g.transpose()
// Step 3: Perform DFS on the transposed graph in the order defined by the stack.
visited = make([]bool, g.vertices)
var sccs [][]int
for len(stack) > 0 {
// Pop vertex from stack
v := stack[len(stack)-1]
stack = stack[:len(stack)-1]
// Perform DFS if not already visited.
if !visited[v] {
scc := []int{}
transposed.dfs(v, visited, &scc)
sccs = append(sccs, scc)
}
}
return sccs
}
// Helper function to fill the stack with vertices in the order of their finish times.
func (g *Graph) fillOrder(v int, visited []bool, stack *[]int) {
visited[v] = true
for neighbor := range g.edges[v] {
if !visited[neighbor] {
g.fillOrder(neighbor, visited, stack)
}
}
// Push the current vertex to the stack after exploring all neighbors.
*stack = append(*stack, v)
}
// Helper function to create a transposed (reversed) graph.
func (g *Graph) transpose() *Graph {
transposed := &Graph{
vertices: g.vertices,
edges: make(map[int]map[int]int),
}
for v, neighbors := range g.edges {
for neighbor := range neighbors {
if transposed.edges[neighbor] == nil {
transposed.edges[neighbor] = make(map[int]int)
}
transposed.edges[neighbor][v] = 1 // Add the reversed edge
}
}
return transposed
}
// Helper DFS function used in the transposed graph to collect SCCs.
func (g *Graph) dfs(v int, visited []bool, scc *[]int) {
visited[v] = true
*scc = append(*scc, v)
for neighbor := range g.edges[v] {
if !visited[neighbor] {
g.dfs(neighbor, visited, scc)
}
}
}
================================================
FILE: graph/kosaraju_test.go
================================================
package graph
import (
"reflect"
"sort"
"testing"
)
func TestKosaraju(t *testing.T) {
tests := []struct {
name string
vertices int
edges map[int][]int
expected [][]int
}{
{
name: "Single SCC",
vertices: 5,
edges: map[int][]int{
0: {1},
1: {2},
2: {0, 3},
3: {4},
4: {},
},
expected: [][]int{{4}, {3}, {0, 2, 1}},
},
{
name: "Multiple SCCs",
vertices: 8,
edges: map[int][]int{
0: {1},
1: {2},
2: {0, 3},
3: {4},
4: {5},
5: {3, 6},
6: {7},
7: {6},
},
expected: [][]int{{6, 7}, {3, 4, 5}, {0, 2, 1}},
},
{
name: "Disconnected graph",
vertices: 4,
edges: map[int][]int{
0: {1},
1: {},
2: {3},
3: {},
},
expected: [][]int{{1}, {0}, {3}, {2}},
},
{
name: "No edges",
vertices: 3,
edges: map[int][]int{
0: {},
1: {},
2: {},
},
expected: [][]int{{0}, {1}, {2}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Initializing graph
graph := &Graph{
vertices: tt.vertices,
edges: make(map[int]map[int]int),
}
for v, neighbors := range tt.edges {
graph.edges[v] = make(map[int]int)
for _, neighbor := range neighbors {
graph.edges[v][neighbor] = 1
}
}
// Running Kosaraju's algorithm to get the SCCs
result := graph.Kosaraju()
// Sort the expected and result SCCs to ensure order doesn't matter
sortSlices(tt.expected)
sortSlices(result)
// Compare the sorted SCCs
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}
// Utility function to sort the slices and their contents
func sortSlices(s [][]int) {
for _, inner := range s {
sort.Ints(inner)
}
sort.Slice(s, func(i, j int) bool {
if len(s[i]) == 0 || len(s[j]) == 0 {
return len(s[i]) < len(s[j])
}
return s[i][0] < s[j][0]
})
}
================================================
FILE: graph/kruskal.go
================================================
// KRUSKAL'S ALGORITHM
// Reference: Kruskal's Algorithm: https://www.scaler.com/topics/data-structures/kruskal-algorithm/
// Reference: Union Find Algorithm: https://www.scaler.com/topics/data-structures/disjoint-set/
// Author: Author: Mugdha Behere[https://github.com/MugdhaBehere]
// Worst Case Time Complexity: O(E log E), where E is the number of edges.
// Worst Case Space Complexity: O(V + E), where V is the number of vertices and E is the number of edges.
// see kruskal.go, kruskal_test.go
package graph
import (
"sort"
)
type Vertex int
type Edge struct {
Start Vertex
End Vertex
Weight int
}
func KruskalMST(n int, edges []Edge) ([]Edge, int) {
// Initialize variables to store the minimum spanning tree and its total cost
var mst []Edge
var cost int
// Create a new UnionFind data structure with 'n' nodes
u := NewUnionFind(n)
// Sort the edges in non-decreasing order based on their weights
sort.SliceStable(edges, func(i, j int) bool {
return edges[i].Weight < edges[j].Weight
})
// Iterate through the sorted edges
for _, edge := range edges {
// Check if adding the current edge forms a cycle or not
if u.Find(int(edge.Start)) != u.Find(int(edge.End)) {
// Add the edge to the minimum spanning tree
mst = append(mst, edge)
// Add the weight of the edge to the total cost
cost += edge.Weight
// Merge the sets containing the start and end vertices of the current edge
u.Union(int(edge.Start), int(edge.End))
}
}
// Return the minimum spanning tree and its total cost
return mst, cost
}
================================================
FILE: graph/kruskal_test.go
================================================
package graph
import (
"fmt"
"testing"
)
func TestKruskalMST(t *testing.T) {
// Define test cases with inputs, expected outputs, and sample graphs
var testCases = []struct {
n int
graph []Edge
cost int
}{
// Test Case 1
{
n: 5,
graph: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 0, End: 2, Weight: 13},
{Start: 0, End: 3, Weight: 7},
{Start: 0, End: 4, Weight: 7},
{Start: 1, End: 2, Weight: 9},
{Start: 1, End: 3, Weight: 3},
{Start: 1, End: 4, Weight: 7},
{Start: 2, End: 3, Weight: 10},
{Start: 2, End: 4, Weight: 14},
{Start: 3, End: 4, Weight: 4},
},
cost: 20,
},
// Test Case 2
{
n: 3,
graph: []Edge{
{Start: 0, End: 1, Weight: 12},
{Start: 0, End: 2, Weight: 18},
{Start: 1, End: 2, Weight: 6},
},
cost: 18,
},
// Test Case 3
{
n: 4,
graph: []Edge{
{Start: 0, End: 1, Weight: 2},
{Start: 0, End: 2, Weight: 1},
{Start: 0, End: 3, Weight: 2},
{Start: 1, End: 2, Weight: 1},
{Start: 1, End: 3, Weight: 2},
{Start: 2, End: 3, Weight: 3},
},
cost: 4,
},
// Test Case 4
{
n: 2,
graph: []Edge{
{Start: 0, End: 1, Weight: 4000000},
},
cost: 4000000,
},
// Test Case 5
{
n: 1,
graph: []Edge{
{Start: 0, End: 0, Weight: 0},
},
cost: 0,
},
}
// Iterate through the test cases and run the tests
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test Case %d", i), func(t *testing.T) {
// Execute KruskalMST for the current test case
_, computed := KruskalMST(testCase.n, testCase.graph)
// Compare the computed result with the expected result
if computed != testCase.cost {
t.Errorf("Test Case %d, Expected: %d, Computed: %d", i, testCase.cost, computed)
}
})
}
}
================================================
FILE: graph/lowestcommonancestor.go
================================================
// lowestcommonancestor.go
// description: Implementation of Lowest common ancestor (LCA) algorithm.
// detail:
// Let `T` be a tree. The LCA of `u` and `v` in T is the shared ancestor of `u` and `v`
// that is located farthest from the root.
// time complexity: O(n log n) where n is the number of vertices in the tree
// space complexity: O(n log n) where n is the number of vertices in the tree
// references: [cp-algorithms](https://cp-algorithms.com/graph/lca_binary_lifting.html)
// author(s) [Dat](https://github.com/datbeohbbh)
// see lowestcommonancestor_test.go for a test implementation.
package graph
type TreeEdge struct {
from int
to int
}
type ITree interface {
dfs(int, int)
addEdge(int, int)
GetDepth(int) int
GetDad(int) int
GetLCA(int, int) int
}
type Tree struct {
numbersVertex int
root int
MAXLOG int
depth []int
dad []int
jump [][]int
edges [][]int
}
func (tree *Tree) addEdge(u, v int) {
tree.edges[u] = append(tree.edges[u], v)
tree.edges[v] = append(tree.edges[v], u)
}
func (tree *Tree) dfs(u, par int) {
tree.jump[0][u] = par
tree.dad[u] = par
for _, v := range tree.edges[u] {
if v != par {
tree.depth[v] = tree.depth[u] + 1
tree.dfs(v, u)
}
}
}
func (tree *Tree) GetDepth(u int) int {
return tree.depth[u]
}
func (tree *Tree) GetDad(u int) int {
return tree.dad[u]
}
func (tree *Tree) GetLCA(u, v int) int {
if tree.GetDepth(u) < tree.GetDepth(v) {
u, v = v, u
}
for j := tree.MAXLOG - 1; j >= 0; j-- {
if tree.GetDepth(tree.jump[j][u]) >= tree.GetDepth(v) {
u = tree.jump[j][u]
}
}
if u == v {
return u
}
for j := tree.MAXLOG - 1; j >= 0; j-- {
if tree.jump[j][u] != tree.jump[j][v] {
u = tree.jump[j][u]
v = tree.jump[j][v]
}
}
return tree.jump[0][u]
}
func NewTree(numbersVertex, root int, edges []TreeEdge) (tree *Tree) {
tree = new(Tree)
tree.numbersVertex, tree.root, tree.MAXLOG = numbersVertex, root, 0
tree.depth = make([]int, numbersVertex)
tree.dad = make([]int, numbersVertex)
for (1 << tree.MAXLOG) <= numbersVertex {
(tree.MAXLOG) += 1
}
(tree.MAXLOG) += 1
tree.jump = make([][]int, tree.MAXLOG)
for j := 0; j < tree.MAXLOG; j++ {
tree.jump[j] = make([]int, numbersVertex)
}
tree.edges = make([][]int, numbersVertex)
for _, e := range edges {
tree.addEdge(e.from, e.to)
}
return tree
}
// For each node, we will precompute its ancestor above him, its ancestor two nodes above, its ancestor four nodes above, etc.
// Let's call `jump[j][u]` is the `2^j`-th ancestor above the node `u` with `u` in range `[0, numbersVertex)`, `j` in range `[0,MAXLOG)`.
// These information allow us to jump from any node to any ancestor above it in `O(MAXLOG)` time.
func LowestCommonAncestor(tree *Tree) {
// call dfs to compute depth from the root to each node and the parent of each node.
tree.dfs(tree.root, tree.root)
// compute jump[j][u]
for j := 1; j < tree.MAXLOG; j++ {
for u := 0; u < tree.numbersVertex; u++ {
tree.jump[j][u] = tree.jump[j-1][tree.jump[j-1][u]]
}
}
}
================================================
FILE: graph/lowestcommonancestor_test.go
================================================
package graph
import (
"math/rand"
"testing"
"time"
)
type Query struct {
u int
v int
expected int
}
func TestLCA(t *testing.T) {
testSuites := []struct {
numbersVertex int
root int
edges []TreeEdge
queries []Query
}{
{
numbersVertex: 2,
root: 0,
edges: []TreeEdge{
{
from: 0,
to: 1,
},
},
queries: []Query{
{
u: 0,
v: 0,
expected: 0,
},
{
u: 0,
v: 1,
expected: 0,
},
{
u: 1,
v: 1,
expected: 1,
},
},
}, {
numbersVertex: 1,
root: 0,
edges: []TreeEdge{},
queries: []Query{
{
u: 0,
v: 0,
expected: 0,
},
},
}, {
numbersVertex: 9,
root: 7,
edges: []TreeEdge{
{
from: 0,
to: 1,
},
{
from: 0,
to: 2,
},
{
from: 2,
to: 3,
},
{
from: 2,
to: 4,
},
{
from: 2,
to: 6,
},
{
from: 3,
to: 5,
},
{
from: 6,
to: 7,
},
{
from: 6,
to: 8,
},
},
queries: []Query{
{
u: 1,
v: 0,
expected: 0,
},
{
u: 8,
v: 2,
expected: 6,
},
{
u: 1,
v: 5,
expected: 2,
},
{
u: 4,
v: 7,
expected: 7,
},
{
u: 0,
v: 8,
expected: 6,
},
},
},
}
// Test #2:
// 7
// /
// 6
// / \
// 2 8
// / | \
// 0 3 4
// / |
// 1 5
for idx, test := range testSuites {
tree := NewTree(test.numbersVertex, test.root, test.edges)
LowestCommonAncestor(tree)
for qi, query := range test.queries {
actual := tree.GetLCA(query.u, query.v)
expected := query.expected
if actual != expected {
t.Errorf("\nTest #%d:\nQuery #%d: u = %d, v = %d\nExpected %d, but actual %d", idx, qi, query.u, query.v, expected, actual)
}
}
}
}
func generateTree() *Tree {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
const MAXVERTEX int = 2000
var numbersVertex int = rnd.Intn(MAXVERTEX) + 1
var root int = rnd.Intn(numbersVertex)
var edges []TreeEdge
var fullGraph []TreeEdge
for u := 0; u < numbersVertex; u++ {
for v := 0; v < numbersVertex; v++ {
fullGraph = append(fullGraph, TreeEdge{
from: u,
to: v,
})
}
}
rnd.Shuffle(len(fullGraph), func(i, j int) {
fullGraph[i], fullGraph[j] = fullGraph[j], fullGraph[i]
})
par := make([]int, numbersVertex)
for u := 0; u < numbersVertex; u++ {
par[u] = u
}
var findp func(int) int
findp = func(u int) int {
if u == par[u] {
return u
} else {
par[u] = findp(par[u])
return par[u]
}
}
join := func(u, v int) bool {
u, v = findp(u), findp(v)
if u == v {
return false
}
par[v] = u
return true
}
for _, e := range fullGraph {
if join(e.from, e.to) == true {
edges = append(edges, e)
}
}
return NewTree(numbersVertex, root, edges)
}
func generateQuery(tree *Tree) []Query {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
const MAXQUERY = 50
var queries []Query
bruteforceLCA := func(u, v int) int {
for u != v {
if tree.GetDepth(u) > tree.GetDepth(v) {
u = tree.GetDad(u)
} else {
v = tree.GetDad(v)
}
}
return u
}
for q := 1; q <= MAXQUERY; q++ {
u := rnd.Intn(tree.numbersVertex)
v := rnd.Intn(tree.numbersVertex)
queries = append(queries, Query{
u: u,
v: v,
expected: bruteforceLCA(u, v),
})
}
return queries
}
// Test with the tree with up to 2000 vertices.
func TestLCAWithLargeTree(t *testing.T) {
const MAXTEST int = 20
for test := 1; test <= MAXTEST; test++ {
tree := generateTree()
LowestCommonAncestor(tree)
queries := generateQuery(tree)
for qi, query := range queries {
actual := tree.GetLCA(query.u, query.v)
expected := query.expected
if actual != expected {
t.Errorf("\nTest #%d:\nQuery #%d: u = %d, v = %d\nExpected %d, but actual %d", test, qi, query.u, query.v, expected, actual)
}
}
}
}
================================================
FILE: graph/prim.go
================================================
// The Prim's algorithm computes the minimum spanning tree for a weighted undirected graph
// Worst Case Time Complexity: O(E log V) using Binary heap, where V is the number of vertices and E is the number of edges
// Space Complexity: O(V + E)
// Implementation is based on the book 'Introduction to Algorithms' (CLRS)
package graph
import (
"container/heap"
)
type minEdge []Edge
func (h minEdge) Len() int { return len(h) }
func (h minEdge) Less(i, j int) bool { return h[i].Weight < h[j].Weight }
func (h minEdge) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *minEdge) Push(x interface{}) {
*h = append(*h, x.(Edge))
}
func (h *minEdge) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func (g *Graph) PrimMST(start Vertex) ([]Edge, int) {
var mst []Edge
marked := make([]bool, g.vertices)
h := &minEdge{}
// Pushing neighbors of the start node to the binary heap
for neighbor, weight := range g.edges[int(start)] {
heap.Push(h, Edge{start, Vertex(neighbor), weight})
}
marked[start] = true
cost := 0
for h.Len() > 0 {
e := heap.Pop(h).(Edge)
end := int(e.End)
// To avoid cycles
if marked[end] {
continue
}
marked[end] = true
cost += e.Weight
mst = append(mst, e)
// Check for neighbors of the newly added edge's End vertex
for neighbor, weight := range g.edges[end] {
if !marked[neighbor] {
heap.Push(h, Edge{e.End, Vertex(neighbor), weight})
}
}
}
return mst, cost
}
================================================
FILE: graph/prim_test.go
================================================
package graph
import (
"fmt"
"reflect"
"testing"
)
func TestPrimMST(t *testing.T) {
var testCases = []struct {
edges []Edge
vertices int
start int
cost int
mst []Edge
}{
{
edges: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 0, End: 2, Weight: 13},
{Start: 0, End: 3, Weight: 7},
{Start: 0, End: 4, Weight: 7},
{Start: 1, End: 2, Weight: 9},
{Start: 1, End: 3, Weight: 3},
{Start: 1, End: 4, Weight: 7},
{Start: 2, End: 3, Weight: 10},
{Start: 2, End: 4, Weight: 14},
{Start: 3, End: 4, Weight: 4},
},
vertices: 5,
start: 0,
cost: 20,
mst: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 1, End: 3, Weight: 3},
{Start: 3, End: 4, Weight: 4},
{Start: 1, End: 2, Weight: 9},
},
},
{
edges: []Edge{
{Start: 0, End: 1, Weight: 4},
{Start: 0, End: 7, Weight: 8},
{Start: 1, End: 2, Weight: 8},
{Start: 1, End: 7, Weight: 11},
{Start: 2, End: 3, Weight: 7},
{Start: 2, End: 5, Weight: 4},
{Start: 2, End: 8, Weight: 2},
{Start: 3, End: 4, Weight: 9},
{Start: 3, End: 5, Weight: 14},
{Start: 4, End: 5, Weight: 10},
{Start: 5, End: 6, Weight: 2},
{Start: 6, End: 7, Weight: 1},
{Start: 6, End: 8, Weight: 6},
{Start: 7, End: 8, Weight: 7},
},
vertices: 9,
start: 3,
cost: 37,
mst: []Edge{
{Start: 3, End: 2, Weight: 7},
{Start: 2, End: 8, Weight: 2},
{Start: 2, End: 5, Weight: 4},
{Start: 5, End: 6, Weight: 2},
{Start: 6, End: 7, Weight: 1},
{Start: 2, End: 1, Weight: 8},
{Start: 1, End: 0, Weight: 4},
{Start: 3, End: 4, Weight: 9},
},
},
{
edges: []Edge{
{Start: 0, End: 1, Weight: 2},
{Start: 0, End: 3, Weight: 6},
{Start: 1, End: 2, Weight: 3},
{Start: 1, End: 3, Weight: 8},
{Start: 1, End: 4, Weight: 5},
{Start: 2, End: 4, Weight: 7},
{Start: 3, End: 4, Weight: 9},
},
vertices: 5,
start: 2,
cost: 16,
mst: []Edge{
{Start: 2, End: 1, Weight: 3},
{Start: 1, End: 0, Weight: 2},
{Start: 1, End: 4, Weight: 5},
{Start: 0, End: 3, Weight: 6},
},
},
{
edges: []Edge{
{Start: 0, End: 0, Weight: 0},
},
vertices: 1,
start: 0,
cost: 0,
mst: nil,
},
{
edges: []Edge{
{Start: 0, End: 1, Weight: 1},
{Start: 0, End: 2, Weight: 6},
{Start: 0, End: 3, Weight: 5},
{Start: 1, End: 2, Weight: 2},
{Start: 1, End: 4, Weight: 4},
{Start: 2, End: 4, Weight: 9},
},
vertices: 5,
start: 4,
cost: 12,
mst: []Edge{
{Start: 4, End: 1, Weight: 4},
{Start: 1, End: 0, Weight: 1},
{Start: 1, End: 2, Weight: 2},
{Start: 0, End: 3, Weight: 5},
},
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test Case %d", i), func(t *testing.T) {
// Initializing graph, adding edges
graph := New(testCase.vertices)
graph.Directed = false
for _, edge := range testCase.edges {
graph.AddWeightedEdge(int(edge.Start), int(edge.End), edge.Weight)
}
computedMST, computedCost := graph.PrimMST(Vertex(testCase.start))
// Compare the computed result with the expected result
if computedCost != testCase.cost {
t.Errorf("Test Case %d, Expected Cost: %d, Computed: %d", i, testCase.cost, computedCost)
}
if !reflect.DeepEqual(testCase.mst, computedMST) {
t.Errorf("Test Case %d, Expected MST: %v, Computed: %v", i, testCase.mst, computedMST)
}
})
}
}
================================================
FILE: graph/topological.go
================================================
// topological.go
// description: Topological sort
// details: Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.
// time complexity: O(V+E) where V is the number of vertices and E is the number of edges in the graph
// space complexity: O(V) where V is the number of vertices in the graph
// reference: https://en.wikipedia.org/wiki/Topological_sorting
package graph
// Topological assumes that graph given is valid and that its
// possible to get a topological ordering.
// constraints are array of []int{a, b}, representing
// an edge going from a to b
func Topological(N int, constraints [][]int) []int {
dependencies := make([]int, N)
nodes := make([]int, N)
for i := range nodes {
nodes[i] = i
}
edges := make([][]bool, N)
for i := range edges {
edges[i] = make([]bool, N)
}
for _, c := range constraints {
a := c[0]
b := c[1]
dependencies[b]++
edges[a][b] = true
}
var answer []int
for s := 0; s < N; s++ {
// Only start walking from top level nodes
if dependencies[s] == 0 {
route, _ := DepthFirstSearchHelper(s, N, nodes, edges, true)
answer = append(answer, route...)
}
}
return answer
}
================================================
FILE: graph/topological_test.go
================================================
package graph
import (
"testing"
)
var testCases = []struct {
name string
N int
constraints [][]int
}{
{
"basic test", 2,
[][]int{{1, 0}},
},
{
"double path", 7,
[][]int{
{0, 1}, {1, 3}, {3, 5},
{0, 2}, {2, 4}, {4, 6}},
},
{
"star shape", 7,
[][]int{
{0, 1}, {0, 3}, {0, 5},
{0, 2}, {0, 4}, {0, 6}},
},
{
"tree shape", 7,
[][]int{
{0, 1}, {1, 3}, {1, 5},
{0, 2}, {2, 4}, {2, 6}},
},
}
func TestTopological(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := Topological(tc.N, tc.constraints)
visited := make([]bool, tc.N)
positions := make([]int, tc.N)
for i := 0; i < tc.N; i++ {
positions[actual[i]] = i
visited[actual[i]] = true
}
for _, v := range visited {
if !v {
t.Errorf("nodes not all visited, %v", visited)
}
}
for _, c := range tc.constraints {
if positions[c[0]] > positions[c[1]] {
t.Errorf("%v dun satisfy %v", actual, c)
}
}
})
}
}
================================================
FILE: graph/unionfind.go
================================================
// Union Find Algorithm or Dynamic Connectivity algorithm, often implemented with the help
//of the union find data structure,
// is used to efficiently maintain connected components in a graph that undergoes dynamic changes,
// such as edges being added or removed over time
// Worst Case Time Complexity: The time complexity of find operation is nearly constant or
//O(α(n)), where α(n) is the inverse Ackermann function
// practically, this is a very slowly growing function making the time complexity for find
//operation nearly constant.
// The time complexity of the union operation is also nearly constant or O(α(n))
// Worst Case Space Complexity: O(n), where n is the number of nodes or element in the structure
// Reference: https://www.scaler.com/topics/data-structures/disjoint-set/
// https://en.wikipedia.org/wiki/Disjoint-set_data_structure
// Author: Mugdha Behere[https://github.com/MugdhaBehere]
// see: unionfind.go, unionfind_test.go
package graph
// Defining the union-find data structure
type UnionFind struct {
parent []int
rank []int
}
// Initialise a new union find data structure with s nodes
func NewUnionFind(s int) UnionFind {
parent := make([]int, s)
rank := make([]int, s)
for i := 0; i < s; i++ {
parent[i] = i
rank[i] = 1
}
return UnionFind{parent, rank}
}
// Find finds the root of the set to which the given element belongs.
// It performs path compression to make future Find operations faster.
func (u *UnionFind) Find(q int) int {
if q != u.parent[q] {
u.parent[q] = u.Find(u.parent[q])
}
return u.parent[q]
}
// Union merges the sets, if not already merged, to which the given elements belong.
// It performs union by rank to keep the tree as flat as possible.
func (u *UnionFind) Union(p, q int) {
rootP := u.Find(p)
rootQ := u.Find(q)
if rootP == rootQ {
return
}
if u.rank[rootP] < u.rank[rootQ] {
u.parent[rootP] = rootQ
} else if u.rank[rootP] > u.rank[rootQ] {
u.parent[rootQ] = rootP
} else {
u.parent[rootQ] = rootP
u.rank[rootP]++
}
}
================================================
FILE: graph/unionfind_test.go
================================================
package graph
import (
"testing"
)
func TestUnionFind(t *testing.T) {
u := NewUnionFind(10) // Creating a Union-Find data structure with 10 elements
//union operations
u.Union(0, 1)
u.Union(2, 3)
u.Union(4, 5)
u.Union(6, 7)
// Testing the parent of specific elements
t.Run("Test Find", func(t *testing.T) {
if u.Find(0) != u.Find(1) || u.Find(2) != u.Find(3) || u.Find(4) != u.Find(5) || u.Find(6) != u.Find(7) {
t.Error("Union operation not functioning correctly")
}
})
u.Union(1, 5) // Additional union operation
u.Union(3, 7) // Additional union operation
// Testing the parent of specific elements after more union operations
t.Run("Test Find after Union", func(t *testing.T) {
if u.Find(0) != u.Find(5) || u.Find(1) != u.Find(4) || u.Find(2) != u.Find(7) || u.Find(3) != u.Find(6) {
t.Error("Union operation not functioning correctly")
}
})
u.Union(3, 7) // Repeated union operation
// Testing that repeated union operations are idempotent
t.Run("Test Find after repeated Union", func(t *testing.T) {
if u.Find(2) != u.Find(6) || u.Find(2) != u.Find(7) || u.Find(3) != u.Find(6) || u.Find(3) != u.Find(7) {
t.Error("Union operation not functioning correctly")
}
})
}
================================================
FILE: hashing/doc.go
================================================
// Package hashing containing different implementation of certain hashing
package hashing
================================================
FILE: hashing/hashing_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package hashing
================================================
FILE: hashing/md5/md5.go
================================================
// md5.go
// description: The MD5 hashing function as defined in RFC 1321.
// author: Simon Waldherr
// ref: https://datatracker.ietf.org/doc/html/rfc1321
// see md5_test.go for testing
package md5
import (
"encoding/binary"
)
// Constants for MD5
var (
s = [64]uint32{
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
}
K = [64]uint32{
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
}
)
// leftRotate rotates x left by n bits
func leftRotate(x, n uint32) uint32 {
return (x << n) | (x >> (32 - n))
}
// pad pads the input message so that its length is congruent to 448 modulo 512
func pad(message []byte) []byte {
originalLength := len(message) * 8
message = append(message, 0x80)
for (len(message)*8)%512 != 448 {
message = append(message, 0x00)
}
lengthBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(lengthBytes, uint64(originalLength))
message = append(message, lengthBytes...)
return message
}
// Hash computes the MD5 hash of the input message
func Hash(message []byte) [16]byte {
message = pad(message)
// Initialize MD5 state variables
a0, b0, c0, d0 := uint32(0x67452301), uint32(0xefcdab89), uint32(0x98badcfe), uint32(0x10325476)
// Process the message in successive 512-bit chunks
for i := 0; i < len(message); i += 64 {
chunk := message[i : i+64]
var M [16]uint32
for j := 0; j < 16; j++ {
M[j] = binary.LittleEndian.Uint32(chunk[j*4 : (j+1)*4])
}
// Initialize hash value for this chunk
A, B, C, D := a0, b0, c0, d0
// Main loop
for i := 0; i < 64; i++ {
var F, g uint32
if i < 16 {
F = (B & C) | ((^B) & D)
g = uint32(i)
} else if i < 32 {
F = (D & B) | ((^D) & C)
g = uint32((5*i + 1) % 16)
} else if i < 48 {
F = B ^ C ^ D
g = uint32((3*i + 5) % 16)
} else {
F = C ^ (B | (^D))
g = uint32((7 * i) % 16)
}
F = F + A + K[i] + M[g]
A = D
D = C
C = B
B = B + leftRotate(F, s[i])
}
// Add this chunk's hash to result so far
a0 += A
b0 += B
c0 += C
d0 += D
}
// Produce the final hash value (digest)
var digest [16]byte
binary.LittleEndian.PutUint32(digest[0:4], a0)
binary.LittleEndian.PutUint32(digest[4:8], b0)
binary.LittleEndian.PutUint32(digest[8:12], c0)
binary.LittleEndian.PutUint32(digest[12:16], d0)
return digest
}
================================================
FILE: hashing/md5/md5_test.go
================================================
// md5_test.go
// description: Tests for the MD5 hashing function as defined in RFC 1321.
// author: Simon Waldherr
package md5
import (
"encoding/hex"
"testing"
)
// Helper function to convert hash output to hex string for comparison
func toHexString(hash [16]byte) string {
return hex.EncodeToString(hash[:])
}
// Test vectors for MD5 (from RFC 1321 and other known sources)
var tests = []struct {
input string
expected string
}{
{"", "d41d8cd98f00b204e9800998ecf8427e"},
{"a", "0cc175b9c0f1b6a831c399e269772661"},
{"abc", "900150983cd24fb0d6963f7d28e17f72"},
{"message digest", "f96b697d7cb7938d525a2f31aaf161d0"},
{"abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b"},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f"},
{"12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a"},
}
// TestHash verifies that the Hash function produces the correct MD5 hash values
func TestHash(t *testing.T) {
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := Hash([]byte(tt.input))
resultHex := toHexString(result)
if resultHex != tt.expected {
t.Errorf("MD5(%q) = %s; want %s", tt.input, resultHex, tt.expected)
}
})
}
}
================================================
FILE: hashing/sha1/sha1.go
================================================
// sha1.go
// description: The SHA-1 hashing function as defined in RFC 3174.
// author: Simon Waldherr
// ref: https://datatracker.ietf.org/doc/html/rfc3174
// see sha1_test.go for testing
package sha1
import (
"encoding/binary" // Used for interacting with uint at the byte level
)
// Constants for SHA-1
const (
h0 uint32 = 0x67452301
h1 uint32 = 0xEFCDAB89
h2 uint32 = 0x98BADCFE
h3 uint32 = 0x10325476
h4 uint32 = 0xC3D2E1F0
)
// pad pads the input message so that its length is congruent to 448 modulo 512
func pad(message []byte) []byte {
originalLength := len(message) * 8
message = append(message, 0x80)
for (len(message)*8)%512 != 448 {
message = append(message, 0x00)
}
lengthBytes := make([]byte, 8)
binary.BigEndian.PutUint64(lengthBytes, uint64(originalLength))
message = append(message, lengthBytes...)
return message
}
// leftRotate rotates x left by n bits
func leftRotate(x, n uint32) uint32 {
return (x << n) | (x >> (32 - n))
}
// Hash computes the SHA-1 hash of the input message
func Hash(message []byte) [20]byte {
message = pad(message)
// Initialize variables
a, b, c, d, e := h0, h1, h2, h3, h4
// Process the message in successive 512-bit chunks
for i := 0; i < len(message); i += 64 {
var w [80]uint32
chunk := message[i : i+64]
// Break chunk into sixteen 32-bit big-endian words
for j := 0; j < 16; j++ {
w[j] = binary.BigEndian.Uint32(chunk[j*4 : (j+1)*4])
}
// Extend the sixteen 32-bit words into eighty 32-bit words
for j := 16; j < 80; j++ {
w[j] = leftRotate(w[j-3]^w[j-8]^w[j-14]^w[j-16], 1)
}
// Initialize hash value for this chunk
A, B, C, D, E := a, b, c, d, e
// Main loop
for j := 0; j < 80; j++ {
var f, k uint32
switch {
case j < 20:
f = (B & C) | ((^B) & D)
k = 0x5A827999
case j < 40:
f = B ^ C ^ D
k = 0x6ED9EBA1
case j < 60:
f = (B & C) | (B & D) | (C & D)
k = 0x8F1BBCDC
default:
f = B ^ C ^ D
k = 0xCA62C1D6
}
temp := leftRotate(A, 5) + f + E + k + w[j]
E = D
D = C
C = leftRotate(B, 30)
B = A
A = temp
}
// Add this chunk's hash to result so far
a += A
b += B
c += C
d += D
e += E
}
// Produce the final hash value (digest)
var digest [20]byte
binary.BigEndian.PutUint32(digest[0:4], a)
binary.BigEndian.PutUint32(digest[4:8], b)
binary.BigEndian.PutUint32(digest[8:12], c)
binary.BigEndian.PutUint32(digest[12:16], d)
binary.BigEndian.PutUint32(digest[16:20], e)
return digest
}
================================================
FILE: hashing/sha1/sha1_test.go
================================================
// sha1_test.go
// description: Tests for the SHA-1 hashing function as defined in RFC 3174.
// author: Simon Waldherr
package sha1
import (
"encoding/hex"
"testing"
)
// Helper function to convert hash output to hex string for comparison
func toHexString(hash [20]byte) string {
return hex.EncodeToString(hash[:])
}
// Test vectors for SHA-1 (from RFC 3174 and other known sources)
var tests = []struct {
input string
expected string
}{
{"", "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
{"a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8"},
{"abc", "a9993e364706816aba3e25717850c26c9cd0d89d"},
{"message digest", "c12252ceda8be8994d5fa0290a47231c1d16aae3"},
{"abcdefghijklmnopqrstuvwxyz", "32d10c7b8cf96570ca04ce37f2a19d84240d3a89"},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "761c457bf73b14d27e9e9265c46f4b4dda11f940"},
{"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "fecfd28bbc9345891a66d7c1b8ff46e60192d284"},
}
// TestHash verifies that the Hash function produces the correct SHA-1 hash values
func TestHash(t *testing.T) {
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := Hash([]byte(tt.input))
resultHex := toHexString(result)
if resultHex != tt.expected {
t.Errorf("SHA-1(%q) = %s; want %s", tt.input, resultHex, tt.expected)
}
})
}
}
================================================
FILE: hashing/sha256/sha256.go
================================================
// sha256.go
// description: The sha256 cryptographic hash function as defined in the RFC6234 standard.
// time complexity: O(n)
// space complexity: O(n)
// author: [Paul Leydier] (https://github.com/paul-leydier)
// ref: https://datatracker.ietf.org/doc/html/rfc6234
// ref: https://en.wikipedia.org/wiki/SHA-2
// see sha256_test.go
package sha256
import (
"encoding/binary" // Used for interacting with uint at the byte level
"math/bits" // Used for bits rotation operations
)
var K = [64]uint32{
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
}
const chunkSize = 64
// pad returns a padded version of the input message, such as the padded message's length is a multiple
// of 512 bits.
// The padding methodology is as follows:
// A "1" bit is appended at the end of the input message, followed by m "0" bits such as the length is
// 64 bits short of a 512 bits multiple. The remaining 64 bits are filled with the initial length of the
// message, represented as a 64-bits unsigned integer.
// For more details, see: https://datatracker.ietf.org/doc/html/rfc6234#section-4.1
func pad(message []byte) []byte {
L := make([]byte, 8)
binary.BigEndian.PutUint64(L, uint64(len(message)*8))
message = append(message, 0x80) // "1" bit followed by 7 "0" bits
for (len(message)+8)%64 != 0 {
message = append(message, 0x00) // 8 "0" bits
}
message = append(message, L...)
return message
}
// Hash hashes the input message using the sha256 hashing function, and return a 32 byte array.
// The implementation follows the RGC6234 standard, which is documented
// at https://datatracker.ietf.org/doc/html/rfc6234
func Hash(message []byte) [32]byte {
message = pad(message)
// Initialize round constants
h0, h1, h2, h3, h4, h5, h6, h7 := uint32(0x6a09e667), uint32(0xbb67ae85), uint32(0x3c6ef372), uint32(0xa54ff53a),
uint32(0x510e527f), uint32(0x9b05688c), uint32(0x1f83d9ab), uint32(0x5be0cd19)
// Iterate through 512-bit chunks
for chunkStart := 0; chunkStart < len(message); chunkStart += chunkSize {
// Message schedule
var w [64]uint32
for i := 0; i*4 < chunkSize; i++ {
w[i] = binary.BigEndian.Uint32(message[chunkStart+i*4 : chunkStart+(i+1)*4])
}
// Extend the 16 bytes chunk to the whole 64 bytes message schedule
for i := 16; i < 64; i++ {
s0 := bits.RotateLeft32(w[i-15], -7) ^ bits.RotateLeft32(w[i-15], -18) ^ (w[i-15] >> 3)
s1 := bits.RotateLeft32(w[i-2], -17) ^ bits.RotateLeft32(w[i-2], -19) ^ (w[i-2] >> 10)
w[i] = w[i-16] + s0 + w[i-7] + s1
}
// Actual hashing loop
a, b, c, d, e, f, g, h := h0, h1, h2, h3, h4, h5, h6, h7
for i := 0; i < 64; i++ {
S1 := bits.RotateLeft32(e, -6) ^ bits.RotateLeft32(e, -11) ^ bits.RotateLeft32(e, -25)
ch := (e & f) ^ ((^e) & g)
tmp1 := h + S1 + ch + K[i] + w[i]
S0 := bits.RotateLeft32(a, -2) ^ bits.RotateLeft32(a, -13) ^ bits.RotateLeft32(a, -22)
maj := (a & b) ^ (a & c) ^ (b & c)
tmp2 := S0 + maj
h = g
g = f
f = e
e = d + tmp1
d = c
c = b
b = a
a = tmp1 + tmp2
}
h0 += a
h1 += b
h2 += c
h3 += d
h4 += e
h5 += f
h6 += g
h7 += h
}
// Export digest
digest := [32]byte{}
binary.BigEndian.PutUint32(digest[:4], h0)
binary.BigEndian.PutUint32(digest[4:8], h1)
binary.BigEndian.PutUint32(digest[8:12], h2)
binary.BigEndian.PutUint32(digest[12:16], h3)
binary.BigEndian.PutUint32(digest[16:20], h4)
binary.BigEndian.PutUint32(digest[20:24], h5)
binary.BigEndian.PutUint32(digest[24:28], h6)
binary.BigEndian.PutUint32(digest[28:], h7)
return digest
}
================================================
FILE: hashing/sha256/sha256_test.go
================================================
package sha256
import (
"encoding/hex"
"testing"
)
func TestHash(t *testing.T) {
testCases := []struct {
in string
expected string
}{
{"hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"},
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{"a", "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"},
{"The quick brown fox jumps over the lazy dog", "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"},
{"The quick brown fox jumps over the lazy dog.", "ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"},
{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "2d8c2f6d978ca21712b5f6de36c9d31fa8e96a4fa5d8ff8b0188dfb9e7c171bb"},
}
for _, tc := range testCases {
res := Hash([]byte(tc.in))
result := hex.EncodeToString(res[:])
if result != tc.expected {
t.Fatalf("Hash(%s) = %s, expected %s", tc.in, result, tc.expected)
}
}
}
func BenchmarkHash(b *testing.B) {
testCases := []struct {
name string
in string
}{
{"hello world", "hello world"},
{"empty", ""},
{"a", "a"},
{"The quick brown fox jumps over the lazy dog", "The quick brown fox jumps over the lazy dog"},
{"The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog."},
{"Lorem ipsum", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."},
}
for _, testCase := range testCases {
b.Run(testCase.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Hash([]byte(testCase.in))
}
})
}
}
================================================
FILE: math/abs.go
================================================
// abs.go
// description: Absolute value
// details:
// In mathematics, the absolute value or modulus of a real number x, denoted |x|, is the non-negative value of x without regard to its sign. [Absolute value](https://en.wikipedia.org/wiki/Average#Arithmetic_mean)
// author(s) [red_byte](https://github.com/i-redbyte)
// see abs_test.go
package math
// Abs returns absolute value
func Abs(n int) int {
if n < 0 {
return -n
}
return n
}
================================================
FILE: math/abs_test.go
================================================
package math
import (
"github.com/TheAlgorithms/Go/math/binary"
"math"
"testing"
)
func TestAbs(t *testing.T) {
tests := getTests()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := Abs(test.n); got != test.want {
t.Errorf("Abs() = %v, want %v", got, test.want)
}
})
}
}
func getTests() []struct {
name string
n int
want int
} {
tests := []struct {
name string
n int
want int
}{
{"-1 = |1| ", -1, 1},
{"-255 = |255| ", -255, 255},
{"0 = |0| ", 0, 0},
{"5 = |5| ", 5, 5},
{"-5 = |5| ", -5, 5},
{"-98368972 = |98368972| ", -98368972, 98368972},
}
return tests
}
func BenchmarkSimpleAbs(b *testing.B) {
for i := 0; i < b.N; i++ {
Abs(-1024)
}
}
func BenchmarkBinaryAbs32(b *testing.B) {
for i := 0; i < b.N; i++ {
binary.Abs(32, -1024)
}
}
func BenchmarkBinaryAbs64(b *testing.B) {
for i := 0; i < b.N; i++ {
binary.Abs(64, -1024)
}
}
func BenchmarkStdLibAbs(b *testing.B) {
for i := 0; i < b.N; i++ {
math.Abs(-1024)
}
}
================================================
FILE: math/aliquot_test.go
================================================
// aliquotsum_test.go
// description: Returns s(n)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see aliquotsum.go
package math_test
import (
"testing"
"github.com/TheAlgorithms/Go/math"
)
func TestAliquotSum(t *testing.T) {
var tests = []struct {
name string
n int
expectedValue int
expectedError error
}{
{"n = 10", 10, 8, nil},
{"n = 11", 11, 1, nil},
{"n = 1", 1, 0, nil},
{"n = -1", -1, 0, math.ErrPosArgsOnly},
{"n = 0", 0, 0, math.ErrNonZeroArgsOnly},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := math.AliquotSum(test.n)
if result != test.expectedValue || test.expectedError != err {
t.Errorf("expected error: %s, got: %s; expected value: %v, got: %v", test.expectedError, err, test.expectedValue, result)
}
})
}
}
func BenchmarkAliquotSum(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = math.AliquotSum(65536)
}
}
================================================
FILE: math/aliquotsum.go
================================================
// aliquotsum.go
// description: Returns s(n)
// details:
// the aliquot sum s(n) of a positive integer n
// is the sum of all proper divisors of n,
// that is, all divisors of n other than n itself
// wikipedia: https://en.wikipedia.org/wiki/Aliquot_sum
// time complexity: O(n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see aliquotsum_test.go
package math
// This function returns s(n) for given number
func AliquotSum(n int) (int, error) {
switch {
case n < 0:
return 0, ErrPosArgsOnly
case n == 0:
return 0, ErrNonZeroArgsOnly
default:
var sum int
for i := 1; i <= n/2; i++ {
if n%i == 0 {
sum += i
}
}
return sum, nil
}
}
================================================
FILE: math/armstrong/isarmstrong.go
================================================
// isarmstrong.go
// description: Checks if the given number is armstrong number or not
// details: An Armstrong number is a n-digit number that is equal to the sum of each of its digits taken to the nth power.
// time complexity: O(log(n))
// space complexity: O(1)
// ref: https://mathlair.allfunandgames.ca/armstrong.php
// author: Kavitha J
package armstrong
import (
"math"
"strconv"
)
func IsArmstrong(number int) bool {
var rightMost int
var sum int = 0
var tempNum int = number
// to get the number of digits in the number
length := float64(len(strconv.Itoa(number)))
// get the right most digit and break the loop once all digits are iterated
for tempNum > 0 {
rightMost = tempNum % 10
sum += int(math.Pow(float64(rightMost), length))
// update the input digit minus the processed rightMost
tempNum /= 10
}
return number == sum
}
================================================
FILE: math/armstrong/isarmstrong_test.go
================================================
// isarmstrong_test.go
package armstrong
import "testing"
var testCases = []struct {
name string // test description
input int // user input
expected bool // expected return
}{
{
"negative number: Not an armstrong number",
-140,
false,
},
{
"positive number: Not an armstrong number",
23,
false,
},
{
"smallest armstrong number",
0,
true,
},
{
"smallest armstrong number with more than one digit",
153,
true,
},
{
"random armstrong number",
407,
true,
},
{
"random armstrong number",
9474,
true,
},
}
func TestIsArmstrong(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
funcResult := IsArmstrong(test.input)
if test.expected != funcResult {
t.Errorf("Expected answer '%t' for the number '%d' but answer given was %t", test.expected, test.input, funcResult)
}
})
}
}
================================================
FILE: math/binary/abs.go
================================================
// abs.go
// description: returns absolute value using binary operation
// time complexity: O(1)
// space complexity: O(1)
package binary
// Abs returns absolute value using binary operation
// Principle of operation:
// 1) Get the mask by right shift by the base
// 2) Base is the size of an integer variable in bits, for example, for int32 it will be 32, for int64 it will be 64
// 3) For negative numbers, above step sets mask as 1 1 1 1 1 1 1 1 and 0 0 0 0 0 0 0 0 for positive numbers.
// 4) Add the mask to the given number.
// 5) XOR of mask + n and mask gives the absolute value.
func Abs(base, n int) int {
m := n >> (base - 1)
return (n + m) ^ m
}
================================================
FILE: math/binary/abs_test.go
================================================
package binary
import "testing"
func TestAbs(t *testing.T) {
tests := getAbsTests()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := Abs(test.base, test.n); got != test.want {
t.Errorf("Abs() = %v, want %v", got, test.want)
}
})
}
}
func getAbsTests() []struct {
name string
base int
n int
want int
} {
tests := []struct {
name string
base int
n int
want int
}{
{"-1 = |1| ", 32, -1, 1},
{"-255 = |255| ", 32, -255, 255},
{"0 = |0| ", 64, 0, 0},
{"5 = |5| ", 16, 5, 5},
{"-5 = |5| ", 32, -5, 5},
{"-98368972 = |98368972| ", 64, -98368972, 98368972},
{"-110298368972 = |110298368972| ", 64, -98368972, 98368972},
}
return tests
}
================================================
FILE: math/binary/arithmeticmean.go
================================================
// arithmeticmean.go
// description: Arithmetic mean
// details:
// The most common type of average is the arithmetic mean. If n numbers are given, each number denoted by ai (where i = 1,2, ..., n), the arithmetic mean is the sum of the as divided by n or - [Arithmetic mean](https://en.wikipedia.org/wiki/Average#Arithmetic_mean)
// time complexity: O(1)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see arithmeticmean_test.go
// Package binary describes algorithms that use binary operations for different calculations.
package binary
// MeanUsingAndXor This function finds arithmetic mean using "AND" and "XOR" operations
func MeanUsingAndXor(a int, b int) int {
return ((a ^ b) >> 1) + (a & b)
}
// MeanUsingRightShift This function finds arithmetic mean using right shift
func MeanUsingRightShift(a int, b int) int {
return (a + b) >> 1
}
================================================
FILE: math/binary/arithmeticmean_test.go
================================================
// arithmeticmean_test.go
// description: Test for Arithmetic mean
// author(s) [red_byte](https://github.com/i-redbyte)
// see arithmeticmean.go
package binary
import "testing"
func TestMeanUsingAndXor(t *testing.T) {
tests := getTests()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := MeanUsingAndXor(tv.a, tv.b)
if result != tv.result {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
}
})
}
}
func TestMeanUsingRightShift(t *testing.T) {
tests := getTests()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := MeanUsingRightShift(tv.a, tv.b)
if result != tv.result {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
}
})
}
}
func getTests() []struct {
name string
a int
b int
result int
} {
var tests = []struct {
name string
a int
b int
result int
}{
{"Average of 2 and 4", 2, 4, 3},
{"Average of 5 and 5", 5, 5, 5},
{"Average of 1000 and 1002", 1000, 1002, 1001},
{"Average of 80 and 40", 80, 40, 60},
{"Average of 7 and 4", 7, 4, 5},
}
return tests
}
func BenchmarkMeanUsingAndXor(b *testing.B) {
for i := 0; i < b.N; i++ {
MeanUsingAndXor(222, 888)
}
}
func BenchmarkMeanUsingRightShift(b *testing.B) {
for i := 0; i < b.N; i++ {
MeanUsingRightShift(222, 888)
}
}
================================================
FILE: math/binary/bitcounter.go
================================================
// bitcounter.go
// description: Counts the number of set bits in a number
// details:
// For unsigned integer number N, return the number of bits set to 1 - [Bit numbering](https://en.wikipedia.org/wiki/Bit_numbering)
// time complexity: O(log(n))
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitcounter_test.go
package binary
// BitCounter - The function returns the number of set bits for an unsigned integer number
func BitCounter(n uint) int {
counter := 0
for n != 0 {
if n&1 == 1 {
counter++
}
n >>= 1
}
return counter
}
================================================
FILE: math/binary/bitcounter_test.go
================================================
// bitcounter_test.go
// description: Test for counts the number of set bits in a number
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitcounter.go
package binary
import "testing"
func TestBitCounter(t *testing.T) {
tests := []struct {
name string
number uint
want int
}{
{"Number of bits in a number 0", 0, 0},
{"Number of bits in a number 1", 1, 1},
{"Number of bits in a number 255", 255, 8},
{"Number of bits in a number 7", 7, 3},
{"Number of bits in a number 8", 8, 1},
{"Number of bits in a number 9223372036854775807", 9223372036854775807, 63},
{"Number of bits in a number 2147483647", 2147483647, 31},
{"Number of bits in a number 15", 15, 4},
{"Number of bits in a number 16", 16, 1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := BitCounter(test.number); got != test.want {
t.Errorf("BitCounter() = %v, want %v", got, test.want)
}
})
}
}
================================================
FILE: math/binary/checkisnumberpoweroftwo.go
================================================
// checkisnumberpoweroftwo.go
// description: Is the number a power of two
// details:
// Checks if a number is a power of two- [Power of two](https://en.wikipedia.org/wiki/Power_of_two)
// time complexity: O(1)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see checkisnumberpoweroftwo_test.go
package binary
// IsPowerOfTwo This function uses the fact that powers of 2 are represented
// like 10...0 in binary, and numbers one less than the power of 2
// are represented like 11...1.
// Therefore, using the and function:
//
// 10...0
// & 01...1
// 00...0 -> 0
//
// This is also true for 0, which is not a power of 2, for which we
// have to add and extra condition.
func IsPowerOfTwo(x int) bool {
return x > 0 && (x&(x-1)) == 0
}
// IsPowerOfTwoLeftShift This function takes advantage of the fact that left shifting a number
// by 1 is equivalent to multiplying by 2. For example, binary 00000001 when shifted by 3 becomes 00001000,
// which in decimal system is 8 or = 2 * 2 * 2
func IsPowerOfTwoLeftShift(number uint) bool {
for p := uint(1); p <= number; p = p << 1 {
if number == p {
return true
}
}
return false
}
================================================
FILE: math/binary/checkisnumberpoweroftwo_test.go
================================================
// checkisnumberpoweroftwo_test.go
// description: Test for Is the number a power of two
// author(s) [red_byte](https://github.com/i-redbyte)
// see checkisnumberpoweroftwo.go
package binary
import (
math2 "github.com/TheAlgorithms/Go/math"
"math"
"testing"
)
func getTestsForPowerOfTwo() []struct {
name string
a int
missing bool
} {
var tests = []struct {
name string
a int
missing bool
}{
{"Is 64 a power of 2? - YES", 64, true},
{"Is 1 a power of 2? - YES", 1, true},
{"Is 2 a power of 2? - YES", 2, true},
{"Is 5 a power of 2? - NO", 5, false},
{"Is 1023 a power of 2? - NO", 1023, false},
{"Is 1024 a power of 2? - YES", 1024, true},
{"Is 0 a power of 2? - NO", 0, false},
{"Is 9223372036854775807 a power of 2? - NO", math.MaxInt64, false},
{"Is 9223372036854775806 a power of 2? - NO", math.MaxInt64, false},
{"Is 4611686018427387904 a power of 2? - YES", 4611686018427387904, true},
}
return tests
}
func TestIsPowerOfTwo(t *testing.T) {
tests := getTestsForPowerOfTwo()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := IsPowerOfTwo(tv.a)
t.Log(tv.a, " ", result)
if result != tv.missing {
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result)
}
})
}
}
func TestIsPowerOfTwoLeftShift(t *testing.T) {
tests := getTestsForPowerOfTwo()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := IsPowerOfTwoLeftShift(uint(tv.a))
t.Log(tv.a, " ", result)
if result != tv.missing {
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result)
}
})
}
}
func TestIsPowOfTwoUseLog(t *testing.T) {
tests := getTestsForPowerOfTwo()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := math2.IsPowOfTwoUseLog(float64(tv.a))
t.Log(tv.a, " ", result)
if result != tv.missing {
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result)
}
})
}
}
func BenchmarkIsPowerOfTwoBinaryMethod(b *testing.B) {
for i := 0; i < b.N; i++ {
IsPowerOfTwo(1024)
}
}
func BenchmarkIsPowerOfTwoUseCycleAndLeftShift(b *testing.B) {
for i := 0; i < b.N; i++ {
IsPowerOfTwoLeftShift(1024)
}
}
func BenchmarkIsPowerOfTwoUseLog(b *testing.B) {
for i := 0; i < b.N; i++ {
math2.IsPowOfTwoUseLog(1024)
}
}
================================================
FILE: math/binary/fast_inverse_sqrt.go
================================================
// Calculating the inverse square root
// time complexity: O(1)
// space complexity: O(1)
// [See more](https://en.wikipedia.org/wiki/Fast_inverse_square_root)
package binary
import (
"math"
)
// FastInverseSqrt assumes that argument is always positive,
// and it does not deal with negative numbers.
// The "magic" number 0x5f3759df is hex for 1597463007 in decimals.
// The math.Float32bits is alias to *(*uint32)(unsafe.Pointer(&f))
// and math.Float32frombits is to *(*float32)(unsafe.Pointer(&b)).
func FastInverseSqrt(number float32) float32 {
var i uint32
var y, x2 float32
const threehalfs float32 = 1.5
x2 = number * float32(0.5)
y = number
i = math.Float32bits(y) // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1) // magic number and bitshift hacking
y = math.Float32frombits(i)
y = y * (threehalfs - (x2 * y * y)) // 1st iteration of Newton's method
y = y * (threehalfs - (x2 * y * y)) // 2nd iteration, this can be removed
return y
}
================================================
FILE: math/binary/logarithm.go
================================================
// author(s) [red_byte](https://github.com/i-redbyte)
// time complexity: O(1)
// space complexity: O(1)
// see logarithm_test.go
package binary
// LogBase2 Finding the exponent of n = 2**x using bitwise operations (logarithm in base 2 of n) [See more](https://en.wikipedia.org/wiki/Logarithm)
func LogBase2(n uint32) uint32 {
base := [5]uint32{0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}
exponents := [5]uint32{1, 2, 4, 8, 16}
var result uint32
for i := 4; i >= 0; i-- {
if n&base[i] != 0 {
n >>= exponents[i]
result |= exponents[i]
}
}
return result
}
================================================
FILE: math/binary/logarithm_test.go
================================================
// logarithm_test.go
// description: Test for finding the exponent of n = 2**x using bitwise operations (logarithm in base 2 of n)
// author(s) [red_byte](https://github.com/i-redbyte)
// see logarithm.go
package binary
import (
"math"
"testing"
)
func TestLogBase2(t *testing.T) {
tests := []struct {
name string
n uint32
want uint32
}{
{"log2(1) = 0", 1, 0},
{"log2(2) = 1", 2, 1},
{"log2(4) = 2", 4, 2},
{"log2(8) = 3", 8, 3},
{"log2(16) = 4", 16, 4},
{"log2(32) = 5", 32, 5},
{"log2(64) = 6", 64, 6},
{"log2(128) = 7", 128, 7},
{"log2(256) = 8", 256, 8},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := LogBase2(test.n); got != test.want {
t.Errorf("LogBase2() = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkBitwiseLogBase2(b *testing.B) {
for i := 0; i < b.N; i++ {
LogBase2(1024)
}
}
func BenchmarkMathPAckageLogBase2(b *testing.B) {
for i := 0; i < b.N; i++ {
math.Log2(1024)
}
}
================================================
FILE: math/binary/rbc.go
================================================
// rbc.go
// description: Reflected binary code (RBC)
// details:
// The reflected binary code (RBC), also known just as reflected binary (RB) or Gray code after Frank Gray, is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit). - [RBC](https://en.wikipedia.org/wiki/Gray_code)
// time complexity: O(n)
// space complexity: O(n)
// author(s) [red_byte](https://github.com/i-redbyte)
// see rbc_test.go
package binary
// SequenceGrayCode The function generates an "Gray code" sequence of length n
func SequenceGrayCode(n uint) []uint {
result := make([]uint, 0)
var i uint
for i = 0; i < 1<>1))
}
return result
}
================================================
FILE: math/binary/rbc_test.go
================================================
// rbc_test.go
// description: Test for Reflected binary code
// author(s) [red_byte](https://github.com/i-redbyte)
// see rbc.go
package binary
import (
"reflect"
"testing"
)
func TestSequenceGrayCode(t *testing.T) {
tests := []struct {
name string
n uint
want []uint
}{
{"Sequence of values for 1", 1, []uint{0, 1}},
{"Sequence of values for 2", 2, []uint{0, 1, 3, 2}},
{"Sequence of values for 6", 6, []uint{0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16, 48, 49, 51, 50, 54, 55, 53, 52, 60, 61, 63, 62, 58, 59, 57, 56, 40, 41, 43, 42, 46, 47, 45, 44, 36, 37, 39, 38, 34, 35, 33, 32}},
{"Sequence of values for 8", 8, []uint{0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16, 48, 49, 51, 50, 54, 55, 53, 52, 60, 61, 63, 62, 58, 59, 57, 56, 40, 41, 43, 42, 46, 47, 45, 44, 36, 37, 39, 38, 34, 35, 33, 32, 96, 97, 99, 98, 102, 103, 101, 100, 108, 109, 111, 110, 106, 107, 105, 104, 120, 121, 123, 122, 126, 127, 125, 124, 116, 117, 119, 118, 114, 115, 113, 112, 80, 81, 83, 82, 86, 87, 85, 84, 92, 93, 95, 94, 90, 91, 89, 88, 72, 73, 75, 74, 78, 79, 77, 76, 68, 69, 71, 70, 66, 67, 65, 64, 192, 193, 195, 194, 198, 199, 197, 196, 204, 205, 207, 206, 202, 203, 201, 200, 216, 217, 219, 218, 222, 223, 221, 220, 212, 213, 215, 214, 210, 211, 209, 208, 240, 241, 243, 242, 246, 247, 245, 244, 252, 253, 255, 254, 250, 251, 249, 248, 232, 233, 235, 234, 238, 239, 237, 236, 228, 229, 231, 230, 226, 227, 225, 224, 160, 161, 163, 162, 166, 167, 165, 164, 172, 173, 175, 174, 170, 171, 169, 168, 184, 185, 187, 186, 190, 191, 189, 188, 180, 181, 183, 182, 178, 179, 177, 176, 144, 145, 147, 146, 150, 151, 149, 148, 156, 157, 159, 158, 154, 155, 153, 152, 136, 137, 139, 138, 142, 143, 141, 140, 132, 133, 135, 134, 130, 131, 129, 128}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := SequenceGrayCode(test.n); !reflect.DeepEqual(got, test.want) {
t.Errorf("SequenceGrayCode() = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkSequenceGrayCode(b *testing.B) {
for i := 0; i < b.N; i++ {
SequenceGrayCode(6)
}
}
================================================
FILE: math/binary/reversebits.go
================================================
// reversebits.go
// description: Reverse bits
// details:
// Reverse the bits of an integer number
// time complexity: O(log(n))
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see reversebits_test.go
package binary
// ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting
// from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result
// and finally move on to the next bit in the input number.
// Repeat this till all its bits are processed.
func ReverseBits(number uint) uint {
result := uint(0)
intSize := 31
for number != 0 {
result += (number & 1) << intSize
number = number >> 1
intSize -= 1
}
return result
}
================================================
FILE: math/binary/reversebits_test.go
================================================
// reversebits_test.go
// description: Reverse bits
// author(s) [red_byte](https://github.com/i-redbyte)
// see reversebits.go
package binary
import "testing"
func TestReverseBits(t *testing.T) {
var tests = []struct {
name string
number uint
result uint
}{
{"43261596 (00000010100101000001111010011100) to 964176192 (00111001011110000010100101000000)", 43261596, 964176192},
{"3758096384 (11100000000000000000000000000000) to 7 (00000000000000000000000000000111)", 3758096384, 7},
{"7 (00000000000000000000000000000111) to 3758096384 (11100000000000000000000000000000)", 7, 3758096384},
{"2684354560 (10100000000000000000000000000000) to 5 (00000000000000000000000000000101)", 2684354560, 5},
{"2684354561 (10100000000000000000000000000001) to 5 (10000000000000000000000000000101)", 2684354561, 2147483653},
}
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := ReverseBits(tv.number)
t.Log(result)
if result != tv.result {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.result, result)
}
})
}
}
func BenchmarkReverseBits(b *testing.B) {
for i := 0; i < b.N; i++ {
ReverseBits(2684354560)
}
}
================================================
FILE: math/binary/sqrt.go
================================================
// sqrt.go
// description: Square root calculation
// details:
// Calculating the square root using binary operations and a magic number 0x5f3759df [See more](https://en.wikipedia.org/wiki/Fast_inverse_square_root)
// author(s) [red_byte](https://github.com/i-redbyte)
// time complexity: O(1)
// space complexity: O(1)
// see sqrt_test.go
package binary
func Sqrt(n float32) float32 { return 1 / FastInverseSqrt(n) }
================================================
FILE: math/binary/sqrt_test.go
================================================
// sqrt_test.go
// description: Test for square root calculation
// author(s) [red_byte](https://github.com/i-redbyte)
// see sqrt.go
package binary
import (
"math"
"testing"
)
const epsilon = 0.001
func TestSquareRootCalculation(t *testing.T) {
tests := []struct {
name string
number float32
want float64
}{
{"sqrt(1)", 1, 1},
{"sqrt(9)", 9, 3},
{"sqrt(25)", 25, 5},
{"sqrt(121)", 121, 11},
{"sqrt(10000)", 10000, 100},
{"sqrt(169)", 169, 13},
{"sqrt(0)", 0, 0},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Sqrt(test.number)
delta := math.Abs(test.want - float64(got))
if delta > epsilon {
t.Errorf("Sqrt() = %v, want %v delta %v", got, test.want, delta)
}
})
}
}
func BenchmarkSquareRootCalculation(b *testing.B) {
for i := 0; i < b.N; i++ {
Sqrt(225)
}
}
func BenchmarkMathSqrt(b *testing.B) {
for i := 0; i < b.N; i++ {
math.Sqrt(225)
}
}
================================================
FILE: math/binary/xorsearch.go
================================================
// xorsearch.go
// description: Find a missing number in a sequence
// details:
// Given an array A containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. - [xor](https://en.wikipedia.org/wiki/Exclusive_or)
// time complexity: O(n)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see xorsearch_test.go
package binary
// XorSearchMissingNumber This function finds a missing number in a sequence
func XorSearchMissingNumber(a []int) int {
n := len(a)
result := len(a)
for i := 0; i < n; i++ {
result ^= i ^ a[i]
}
return result
}
================================================
FILE: math/binary/xorsearch_test.go
================================================
// xorsearch_test.go
// description: Test for Find a missing number in a sequence
// author(s) [red_byte](https://github.com/i-redbyte)
// see xorsearch.go
package binary
import (
"testing"
)
func TestXorSearchMissingNumber(t *testing.T) {
var tests = []struct {
name string
a []int
missing int
}{
{"[3, 0, 1]/2", []int{3, 0, 1}, 2},
{"[0, 1, 3, 4, 5, 6, 7]/2", []int{0, 1, 3, 4, 5, 6, 7}, 2},
{"[0, 2, 3, 4, 5, 6, 7, 8, 9]/1", []int{0, 2, 3, 4, 5, 6, 7, 8, 9}, 1},
{"[0, 10, 9, 7, 2, 1, 4, 3, 5, 6]/8", []int{0, 10, 9, 7, 2, 1, 4, 3, 5, 6}, 8},
}
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := XorSearchMissingNumber(tv.a)
if result != tv.missing {
t.Errorf("Wrong result! Expected:%d, returned:%d ", tv.missing, result)
}
})
}
}
func BenchmarkTestXorSearchMissingNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
XorSearchMissingNumber([]int{0, 10, 9, 7, 2, 1, 4, 3, 5, 6})
}
}
================================================
FILE: math/binomialcoefficient.go
================================================
// binomialcoefficient.go
// description: Returns C(n, k)
// details:
// a binomial coefficient C(n,k) gives number ways
// in which k objects can be chosen from n objects.
// wikipedia: https://en.wikipedia.org/wiki/Binomial_coefficient
// time complexity: O(k) or O(n-k) whichever is smaller (O(n) in worst case)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see binomialcoefficient_test.go
package math
import (
"errors"
)
var ErrPosArgsOnly error = errors.New("arguments must be positive")
// C is Binomial Coefficient function
// This function returns C(n, k) for given n and k
func Combinations(n int, k int) (int, error) {
if n < 0 || k < 0 {
return -1, ErrPosArgsOnly
}
if k > (n - k) {
k = n - k
}
res := 1
for i := 0; i < k; i++ {
res *= (n - i)
res /= (i + 1)
}
return res, nil
}
================================================
FILE: math/binomialcoefficient_test.go
================================================
// binomialcoefficient_test.go
// description: Returns C(n, k)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see binomialcoefficient.go
package math_test
import (
"testing"
"github.com/TheAlgorithms/Go/math"
)
func TestCombinations(t *testing.T) {
var tests = []struct {
name string
n int
k int
expectedValue int
expectedError error
}{
{"n = 5, k = 2", 5, 2, 10, nil},
{"n = 7, k = 4", 7, 4, 35, nil},
{"n = 0, k = 0", 0, 0, 1, nil},
{"n = -1, k = 1", -1, 1, -1, math.ErrPosArgsOnly},
{"n = 1, k = -1", 1, -1, -1, math.ErrPosArgsOnly},
{"n = -1, k = -1", -1, -1, -1, math.ErrPosArgsOnly},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := math.Combinations(test.n, test.k)
if result != test.expectedValue || test.expectedError != err {
t.Errorf("expected error: %s, got: %s; expected value: %v, got: %v", test.expectedError, err, test.expectedValue, result)
}
})
}
}
func BenchmarkCombinations(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = math.Combinations(65536, 65536)
}
}
================================================
FILE: math/catalan/catalannumber.go
================================================
// catalannumber.go
// description: Returns the Catalan number
// details:
// In combinatorial mathematics, the Catalan numbers are a sequence of natural numbers that occur in various counting problems, often involving recursively defined objects. - [Catalan number](https://en.wikipedia.org/wiki/Catalan_number)
// time complexity: O(n)
// space complexity: O(1)
// The input is the number of the Catalan number n, at the output we get the value of the number
// author(s) [red_byte](https://github.com/i-redbyte)
// see catalannumber_test.go
package catalan
import (
f "github.com/TheAlgorithms/Go/math/factorial"
)
func factorial(n int) int {
result, error := f.Iterative(n)
if error != nil {
panic(error)
}
return result
}
// CatalanNumber This function returns the `nth` Catalan number
func CatalanNumber(n int) int {
return factorial(n*2) / (factorial(n) * factorial(n+1))
}
================================================
FILE: math/catalan/catalannumber_test.go
================================================
// catalannumber_test.go
// description: Test for returns the Catalan number
// author(s) [red_byte](https://github.com/i-redbyte)
// see catalannumber.go
package catalan
import "testing"
func TestCatalanNumber(t *testing.T) {
tests := []struct {
name string
n int
want int
}{
{"zero Catalan number ", 0, 1},
{"second Catalan number ", 2, 2},
{"third Catalan number ", 3, 5},
{"fourth Catalan number ", 4, 14},
{"fifth Catalan number ", 5, 42},
{"sixth Catalan number ", 6, 132},
{"tenth Catalan number ", 10, 16796},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := CatalanNumber(test.n); got != test.want {
t.Errorf("CatalanNumber() = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkCatalanNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
CatalanNumber(10)
}
}
================================================
FILE: math/checkisnumberpoweroftwo.go
================================================
package math
import (
"math"
)
// IsPowOfTwoUseLog This function checks if a number is a power of two using the logarithm.
// The limiting degree can be from 0 to 63.
// See alternatives in the binary package.
func IsPowOfTwoUseLog(number float64) bool {
if number == 0 || math.Round(number) == math.MaxInt64 {
return false
}
log := math.Log2(number)
return log == math.Round(log)
}
================================================
FILE: math/checkisnumberpoweroftwo_test.go
================================================
// checkisnumberpoweroftwo_test.go
// description: Test for Is the number a power of two
// author(s) [red_byte](https://github.com/i-redbyte)
// see checkisnumberpoweroftwo.go
package math
import (
"math"
"testing"
)
func getTestsForPowerOfTwo() []struct {
name string
a int
missing bool
} {
var tests = []struct {
name string
a int
missing bool
}{
{"Is 64 a power of 2? - YES", 64, true},
{"Is 1 a power of 2? - YES", 1, true},
{"Is 2 a power of 2? - YES", 2, true},
{"Is 5 a power of 2? - NO", 5, false},
{"Is 1023 a power of 2? - NO", 1023, false},
{"Is 1024 a power of 2? - YES", 1024, true},
{"Is 0 a power of 2? - NO", 0, false},
{"Is 9223372036854775807 a power of 2? - NO", math.MaxInt64, false},
{"Is 9223372036854775806 a power of 2? - NO", math.MaxInt64, false},
{"Is 4611686018427387904 a power of 2? - YES", 4611686018427387904, true},
}
return tests
}
func TestIsPowOfTwoUseLog(t *testing.T) {
tests := getTestsForPowerOfTwo()
for _, tv := range tests {
t.Run(tv.name, func(t *testing.T) {
result := IsPowOfTwoUseLog(float64(tv.a))
t.Log(tv.a, " ", result)
if result != tv.missing {
t.Errorf("Wrong result! Expected:%v, returned:%v ", tv.missing, result)
}
})
}
}
func BenchmarkIsPowerOfTwoUseLog(b *testing.B) {
for i := 0; i < b.N; i++ {
IsPowOfTwoUseLog(1024)
}
}
================================================
FILE: math/cos.go
================================================
// author(s) [red_byte](https://github.com/i-redbyte)
// see cos_test.go
package math
import "math"
// Cos returns the cosine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine)
// [Based on the idea of Bhaskara approximation of cos(x)](https://math.stackexchange.com/questions/3886552/bhaskara-approximation-of-cosx)
func Cos(x float64) float64 {
tp := 1.0 / (2.0 * math.Pi)
x *= tp
x -= 0.25 + math.Floor(x+0.25)
x *= 16.0 * (math.Abs(x) - 0.5)
x += 0.225 * x * (math.Abs(x) - 1.0) //Extra precision
return x
}
================================================
FILE: math/cos_test.go
================================================
package math_test
import (
algmath "github.com/TheAlgorithms/Go/math"
stdmath "math"
"testing"
)
const epsilon = 0.001
func TestCos(t *testing.T) {
tests := []struct {
name string
n float64
want float64
}{
{"cos(0)", 0, 1},
{"cos(90)", 90, -0.447},
{"cos(180)", 180, -0.598},
{"cos(1)", 1, 0.540},
{"cos(π)", stdmath.Pi, -1},
{"cos(π/2)", stdmath.Pi / 2, 0},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := algmath.Cos(test.n)
if stdmath.Abs(got-test.want) >= epsilon {
t.Errorf("Cos() = %v, want %v", got, test.want)
t.Errorf("MATH Cos() = %v", stdmath.Cos(test.n))
}
})
}
}
func BenchmarkCos(b *testing.B) {
for i := 0; i < b.N; i++ {
algmath.Cos(180)
}
}
// BenchmarkMathCos is slower because the standard library `math.Cos` calculates a more accurate value.
func BenchmarkMathCos(b *testing.B) {
for i := 0; i < b.N; i++ {
stdmath.Cos(180)
}
}
================================================
FILE: math/doc.go
================================================
// Package math is a package that contains mathematical algorithms and its different implementations.
package math
================================================
FILE: math/eulertotient.go
================================================
package math
// Phi is the Euler totient function.
// This function computes the number of numbers less then n that are coprime with n.
func Phi(n int64) int64 {
result := n
for i := int64(2); i*i <= n; i += 1 {
if n%i == 0 {
for {
if n%i != 0 {
break
}
n /= i
}
result -= result / i
}
}
if n > 1 {
result -= result / n
}
return result
}
================================================
FILE: math/eulertotient_test.go
================================================
package math
import (
"testing"
)
func getTestsForPhi() []struct {
n int64
expected int64
} {
var tests = []struct {
n int64
expected int64
}{
{4, 2},
{5, 4},
{7, 6},
{10, 4},
{999, 648},
{1000, 400},
{1000000, 400000},
{999999, 466560},
{999999999999878, 473684210526240},
}
return tests
}
func TestPhi(t *testing.T) {
tests := getTestsForPhi()
for _, test := range tests {
result := Phi(test.n)
t.Log(test.n, " ", result)
if result != test.expected {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.expected, result)
}
}
}
func BenchmarkPhi(b *testing.B) {
for i := 0; i < b.N; i++ {
Phi(65536)
}
}
================================================
FILE: math/factorial/factorial.go
================================================
// factorial.go
// description: Calculating factorial
// details:
// The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n - [Factorial](https://en.wikipedia.org/wiki/Factorial)
// time complexity: O(n)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see factorial_test.go
// Package factorial describes algorithms Factorials calculations.
package factorial
import (
"errors"
)
var ErrNegativeArgument = errors.New("input argument must be non-negative integer")
// Iterative returns the iteratively brute forced factorial of n
func Iterative(n int) (int, error) {
if n < 0 {
return 0, ErrNegativeArgument
}
result := 1
for i := 2; i <= n; i++ {
result *= i
}
return result, nil
}
// Recursive This function recursively computes the factorial of a number
func Recursive(n int) (int, error) {
if n < 0 {
return 0, ErrNegativeArgument
}
if n <= 1 {
return 1, nil
}
prev, _ := Recursive(n - 1)
return n * prev, nil
}
// UsingTree This function finds the factorial of a number using a binary tree
func UsingTree(n int) (int, error) {
if n < 0 {
return 0, ErrNegativeArgument
}
if n == 0 {
return 1, nil
}
if n == 1 || n == 2 {
return n, nil
}
return prodTree(2, n), nil
}
func prodTree(l int, r int) int {
if l > r {
return 1
}
if l == r {
return l
}
if r-l == 1 {
return l * r
}
m := (l + r) / 2
return prodTree(l, m) * prodTree(m+1, r)
}
================================================
FILE: math/factorial/factorial_test.go
================================================
// factorial_test.go
// description: Test for calculating factorial
// see factorial.go
package factorial
import "testing"
import "fmt"
type factorialFun func(int) (int, error)
var implementations = map[string]factorialFun{
"Iterative": Iterative,
"Recursive": Recursive,
"UsingTree": UsingTree,
}
var testCases = []struct {
n int
expected int
}{
{0, 1},
{1, 1},
{2, 2},
{3, 6},
{4, 24},
{5, 120},
{6, 720},
{7, 5040},
{8, 40320},
{9, 362880},
{10, 3628800},
{11, 39916800},
{12, 479001600},
}
func TestFactorial(t *testing.T) {
for implName, implFunction := range implementations {
t.Run(implName+" errors for negative input", func(t *testing.T) {
_, error := implFunction(-1)
if error != ErrNegativeArgument {
t.Errorf("No error captured for negative input")
}
})
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s with input %d", implName, tc.n), func(t *testing.T) {
actual, err := implFunction(tc.n)
if err != nil {
t.Errorf("unexpected error captured")
}
if actual != tc.expected {
t.Errorf("Expected: %d, got: %d", tc.expected, actual)
}
})
}
}
}
func BenchmarkFactorial(b *testing.B) {
for _, input := range []int{5, 10, 15} {
for implName, implFunction := range implementations {
b.Run(fmt.Sprintf("%s_%d", implName, input), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = implFunction(input)
}
})
}
}
}
================================================
FILE: math/fibonacci/fibonacci.go
================================================
// fibonacci.go
// description: Get the nth Fibonacci Number
// details:
// In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number)
// time complexity: O(log n)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see fibonacci_test.go
package fibonacci
import (
"math"
)
// Matrix This function calculates the n-th fibonacci number using the matrix method. [See](https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form)
func Matrix(n uint) uint {
a, b := 1, 1
c, rc, tc := 1, 0, 0
d, rd := 0, 1
for n != 0 {
if n&1 == 1 {
tc = rc
rc = rc*a + rd*c
rd = tc*b + rd*d
}
ta := a
tb := b
tc = c
a = a*a + b*c
b = ta*b + b*d
c = c*ta + d*c
d = tc*tb + d*d
n >>= 1
}
return uint(rc)
}
// Formula This function calculates the n-th fibonacci number using the [formula](https://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio)
// Attention! Tests for large values fall due to rounding error of floating point numbers, works well, only on small numbers
func Formula(n uint) uint {
sqrt5 := math.Sqrt(5)
phi := (sqrt5 + 1) / 2
powPhi := math.Pow(phi, float64(n))
return uint(powPhi/sqrt5 + 0.5)
}
// Recursive calculates the n-th fibonacci number recursively by adding the previous two Fibonacci numbers.
// This algorithm is extremely slow for bigger numbers, but provides a simpler implementation.
func Recursive(n uint) uint {
if n <= 1 {
return n
}
return Recursive(n-1) + Recursive(n-2)
}
================================================
FILE: math/fibonacci/fibonacci_test.go
================================================
package fibonacci
import (
"github.com/TheAlgorithms/Go/dynamic"
"testing"
)
func getTests() []struct {
name string
n uint
want uint
} {
tests := []struct {
name string
n uint
want uint
}{
{"Fibonacci 0-th number == 0", 0, 0},
{"Fibonacci 1-th number == 1", 1, 1},
{"Fibonacci 2-th number == 1", 2, 1},
{"Fibonacci 3-th number == 2", 3, 2},
{"Fibonacci 4-th number == 3", 4, 3},
{"Fibonacci 5-th number == 5", 5, 5},
{"Fibonacci 6-th number == 8", 6, 8},
{"Fibonacci 7-th number == 13", 7, 13},
{"Fibonacci 8-th number == 21", 8, 21},
{"Fibonacci 9-th number == 34", 9, 34},
{"Fibonacci 10-th number == 55", 10, 55},
{"Fibonacci 90-th number == 2880067194370816120", 90, 2880067194370816120},
}
return tests
}
func TestMatrix(t *testing.T) {
tests := getTests()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := Matrix(test.n); got != test.want {
t.Errorf("Return value = %v, want %v", got, test.want)
}
})
}
}
func TestNthFibonacci(t *testing.T) {
tests := getTests()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := dynamic.NthFibonacci(test.n); got != test.want {
t.Errorf("Return value = %v, want %v", got, test.want)
}
})
}
}
func TestFormula(t *testing.T) {
tests := getTests()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := Formula(test.n); test.n <= 10 && got != test.want {
t.Errorf("Return value = %v, want %v", got, test.want)
}
})
}
}
func TestRecursive(t *testing.T) {
tests := getTests()
for _, test := range tests {
if test.n <= 10 {
t.Run(test.name, func(t *testing.T) {
if got := Recursive(test.n); got != test.want {
t.Errorf("Return value = %v, want %v", got, test.want)
}
})
}
}
}
func BenchmarkNthFibonacci(b *testing.B) {
for i := 0; i < b.N; i++ {
dynamic.NthFibonacci(90)
}
}
func BenchmarkMatrix(b *testing.B) {
for i := 0; i < b.N; i++ {
Matrix(90)
}
}
func BenchmarkFormula(b *testing.B) {
for i := 0; i < b.N; i++ {
Formula(90)
}
}
================================================
FILE: math/gcd/extended.go
================================================
// extended.go
// description: Implementation of Extended GCD Algorithm
// details:
// A simple implementation of Extended GCD algorithm, that returns GCD, a and b
// which solves ax + by = gcd(a, b)
// time complexity: O(log(min(a, b))) where a and b are the two numbers
// space complexity: O(log(min(a, b))) where a and b are the two numbers
// author(s) [Taj](https://github.com/tjgurwara99)
// see extended_test.go
package gcd
// Extended simple extended gcd
func Extended(a, b int64) (int64, int64, int64) {
if a == 0 {
return b, 0, 1
}
gcd, xPrime, yPrime := Extended(b%a, a)
return gcd, yPrime - (b/a)*xPrime, xPrime
}
================================================
FILE: math/gcd/extended_test.go
================================================
// extended_test.go
// description: Test for Extended GCD algorithm in extended.go
// author(s) [Taj](https://github.com/tjgurwara99)
// see extended.go
package gcd
import "testing"
func TestExtended(t *testing.T) {
var testCasesExtended = []struct {
name string
a int64
b int64
gcd int64
x int64
y int64
}{
{"gcd of 30 and 50", 30, 50, 10, 2, -1},
}
for _, tc := range testCasesExtended {
t.Run(tc.name, func(t *testing.T) {
gcd, x, y := Extended(tc.a, tc.b)
if gcd != tc.gcd && x != tc.x && y != tc.y {
t.Fatalf("Expected values:\n\tGCD: Expected %v Returned %v,\n\tx: Expected %v Returned %v\n\ty: Expected %v Returned %v", tc.gcd, gcd, tc.x, x, tc.y, y)
}
})
}
}
================================================
FILE: math/gcd/extendedgcd.go
================================================
// extendedgcd.go
// description: Implementation of Extended GCD Algorithm
// time complexity: O(log(min(a, b))) where a and b are the two numbers
// space complexity: O(log(min(a, b))) where a and b are the two numbers
package gcd
// ExtendedRecursive finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b).
func ExtendedRecursive(a, b int64) (int64, int64, int64) {
if b > 0 {
d, y, x := ExtendedRecursive(b, a%b)
y -= (a / b) * x
return d, x, y
}
return a, 1, 0
}
================================================
FILE: math/gcd/extendedgcd_test.go
================================================
package gcd
import "testing"
type testExtendedFunction func(int64, int64) (int64, int64, int64)
func TemplateTestExtendedGCD(t *testing.T, f testExtendedFunction) {
var testCasesExtended = []struct {
name string
a int64
b int64
gcd int64
x int64
y int64
}{
{"gcd of 10 and 0", 10, 0, 10, 1, 0},
{"gcd of 98 and 56", 98, 56, 14, -1, 2},
{"gcd of 0 and 10", 0, 10, 10, 0, 1},
}
for _, tc := range testCasesExtended {
t.Run(tc.name, func(t *testing.T) {
actualGcd, actualX, actualY := f(tc.a, tc.b)
if actualGcd != tc.gcd {
t.Errorf("Expected GCD of %d and %d to be: %v, but got: %d", tc.a, tc.b, tc.gcd, actualGcd)
}
if actualX != tc.x {
t.Errorf("Expected x satisfying %d * x + %d * y = gcd to be: %v, but got: %d", tc.a, tc.b, tc.x, actualX)
}
if actualY != tc.y {
t.Errorf("Expected y satisfying %d * x + %d * y = gcd to be: %v, but got: %d", tc.a, tc.b, tc.y, actualY)
}
})
}
}
func TestExtendedGCDRecursive(t *testing.T) {
TemplateTestExtendedGCD(t, ExtendedRecursive)
}
func TestExtendedGCDIterative(t *testing.T) {
TemplateTestExtendedGCD(t, ExtendedIterative)
}
func TemplateBenchmarkExtendedGCD(b *testing.B, f testExtendedFunction) {
for i := 0; i < b.N; i++ {
f(98, 56)
}
}
func BenchmarkExtendedGCDRecursive(b *testing.B) {
TemplateBenchmarkExtendedGCD(b, ExtendedRecursive)
}
func BenchmarkExtendedGCDIterative(b *testing.B) {
TemplateBenchmarkExtendedGCD(b, ExtendedIterative)
}
================================================
FILE: math/gcd/extendedgcditerative.go
================================================
package gcd
// ExtendedIterative finds and returns gcd(a, b), x, y satisfying a*x + b*y = gcd(a, b).
func ExtendedIterative(a, b int64) (int64, int64, int64) {
var u, y, v, x int64 = 1, 1, 0, 0
for a > 0 {
var q int64 = b / a
x, u = u, x-q*u
y, v = v, y-q*v
b, a = a, b-q*a
}
return b, x, y
}
================================================
FILE: math/gcd/gcd.go
================================================
// time complexity: O(log(min(a, b))) where a and b are the two numbers
// space complexity: O(1)
package gcd
// Recursive finds and returns the greatest common divisor of a given integer.
func Recursive(a, b int64) int64 {
if b == 0 {
return a
}
return Recursive(b, a%b)
}
================================================
FILE: math/gcd/gcd_test.go
================================================
package gcd
import "testing"
type testFunction func(int64, int64) int64
var testCases = []struct {
name string
a int64
b int64
output int64
}{
{"gcd of 10 and 0", 10, 0, 10},
{"gcd of 98 and 56", 98, 56, 14},
{"gcd of 0 and 10", 0, 10, 10},
}
func TemplateTestGCD(t *testing.T, f testFunction) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := f(tc.a, tc.b)
if actual != tc.output {
t.Errorf("Expected GCD of %d and %d to be: %v, but got: %d", tc.a, tc.b, tc.output, actual)
}
})
}
}
func TestGCDRecursive(t *testing.T) {
TemplateTestGCD(t, Recursive)
}
func TestGCDIterative(t *testing.T) {
TemplateTestGCD(t, Iterative)
}
func TemplateBenchmarkGCD(b *testing.B, f testFunction) {
for i := 0; i < b.N; i++ {
f(98, 56)
}
}
func BenchmarkGCDRecursive(b *testing.B) {
TemplateBenchmarkGCD(b, Recursive)
}
func BenchmarkGCDIterative(b *testing.B) {
TemplateBenchmarkGCD(b, Iterative)
}
================================================
FILE: math/gcd/gcditerative.go
================================================
// time complexity: O(log(min(a, b))) where a and b are the two numbers
// space complexity: O(1)
package gcd
// Iterative Faster iterative version of GcdRecursive without holding up too much of the stack
func Iterative(a, b int64) int64 {
for b != 0 {
a, b = b, a%b
}
return a
}
================================================
FILE: math/geometry/distance.go
================================================
// distance.go
// Find Euclidean distance between two points
// time complexity: O(n) where n is the number of dimensions
// space complexity: O(1)
// author(s) [Chetan Patil](https://github.com/Chetan07j)
// Package geometry contains geometric algorithms
package geometry
import (
"errors"
"math"
)
// EuclideanPoint defines a point with x and y coordinates.
type EuclideanPoint []float64
var ErrDimMismatch = errors.New("mismatched dimensions")
// EuclideanDistance returns the Euclidean distance between points in
// any `n` dimensional Euclidean space.
func EuclideanDistance(p1 EuclideanPoint, p2 EuclideanPoint) (float64, error) {
n := len(p1)
if len(p2) != n {
return -1, ErrDimMismatch
}
var total float64 = 0
for i, x_i := range p1 {
// using Abs since the value could be negative but we require the magnitude
diff := math.Abs(x_i - p2[i])
total += diff * diff
}
return math.Sqrt(total), nil
}
================================================
FILE: math/geometry/distance_test.go
================================================
package geometry_test
import (
"errors"
"testing"
geometry "github.com/TheAlgorithms/Go/math/geometry"
)
type args struct {
p1 geometry.EuclideanPoint
p2 geometry.EuclideanPoint
}
func TestFindDistanceBetweenTwoPoints(t *testing.T) {
tests := []struct {
name string
args args
want float64
wantErr bool
}{
{
"(0,0) and (2,-2)",
args{
geometry.EuclideanPoint{0, 0},
geometry.EuclideanPoint{2, -2},
},
2.8284271247461903,
false,
},
{
"(-20,23) and (-15,68)",
args{
geometry.EuclideanPoint{-20, 23},
geometry.EuclideanPoint{-15, 68},
},
45.27692569068709,
false,
},
{
"(2,2) and (14,11)",
args{
geometry.EuclideanPoint{2, 2},
geometry.EuclideanPoint{14, 11},
},
15,
false,
},
{
"Return error for mismatched dimensions(2,2) and ()",
args{
geometry.EuclideanPoint{2, 2},
geometry.EuclideanPoint{},
},
-1,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := geometry.EuclideanDistance(tt.args.p1, tt.args.p2)
if (err != nil) != tt.wantErr && errors.Is(err, geometry.ErrDimMismatch) {
t.Errorf("EuclideanDistance() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("EuclideanDistance() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkFindDistanceBetweenTwoPoints(b *testing.B) {
p1 := geometry.EuclideanPoint{0, 0}
p2 := geometry.EuclideanPoint{2, -2}
for i := 0; i < b.N; i++ {
_, _ = geometry.EuclideanDistance(p1, p2)
}
}
================================================
FILE: math/geometry/straightlines.go
================================================
// Package geometry contains geometric algorithms
package geometry
import (
"math"
)
// Point defines a point with x and y coordinates.
type Point struct {
X, Y float64
}
type Line struct {
P1, P2 Point
}
// Distance calculates the shortest distance between two points.
func Distance(a, b *Point) float64 {
return math.Sqrt(math.Pow(a.X-b.X, 2) + math.Pow(a.Y-b.Y, 2))
}
// Section calculates the Point that divides a line in specific ratio.
// DO NOT specify the ratio in the form m:n, specify it as r, where r = m / n.
func Section(p1, p2 *Point, r float64) Point {
var point Point
point.X = (r*p2.X + p1.X) / (r + 1)
point.Y = (r*p2.Y + p1.Y) / (r + 1)
return point
}
// Slope calculates the slope (gradient) of a line.
func Slope(l *Line) float64 {
return (l.P2.Y - l.P1.Y) / (l.P2.X - l.P1.X)
}
// YIntercept calculates the Y-Intercept of a line from a specific Point.
func YIntercept(p *Point, slope float64) float64 {
return p.Y - (slope * p.X)
}
// IsParallel checks if two lines are parallel or not.
func IsParallel(l1, l2 *Line) bool {
return Slope(l1) == Slope(l2)
}
// IsPerpendicular checks if two lines are perpendicular or not.
func IsPerpendicular(l1, l2 *Line) bool {
return Slope(l1)*Slope(l2) == -1
}
// PointDistance calculates the distance of a given Point from a given line.
// The slice should contain the coefficiet of x, the coefficient of y and the constant in the respective order.
func PointDistance(p *Point, equation [3]float64) float64 {
return math.Abs(equation[0]*p.X+equation[1]*p.Y+equation[2]) / math.Sqrt(math.Pow(equation[0], 2)+math.Pow(equation[1], 2))
}
================================================
FILE: math/geometry/straightlines_test.go
================================================
package geometry
import (
"testing"
)
func TestDistance(t *testing.T) {
p1 := Point{0, 0}
p2 := Point{3, 4}
var wantedDistance float64 = 5
var calculatedDistance float64 = Distance(&p1, &p2)
if calculatedDistance != wantedDistance {
t.Fatalf("Failed to calculate Distance.")
}
}
func TestSection(t *testing.T) {
p1 := Point{1, 0}
p2 := Point{5, 0}
wantedPoint := Point{3, 0}
calculatedPoint := Section(&p1, &p2, 1)
if calculatedPoint != wantedPoint {
t.Fatalf("Failed to calculate Section.")
}
}
func TestSlope(t *testing.T) {
line := Line{P1: Point{1, 2}, P2: Point{2, 4}}
var wantedSlope float64 = 2
var calculatedSlope float64 = Slope(&line)
if calculatedSlope != wantedSlope {
t.Fatalf("Failed to calculate Slope.")
}
}
func TestIntercept(t *testing.T) {
p := Point{0, 3}
var slope float64 = -5
var wantedIntercept float64 = 3
var calculatedIntercept float64 = YIntercept(&p, slope)
if calculatedIntercept != wantedIntercept {
t.Fatalf("Failed to calculate YIntercept.")
}
}
func TestIsParallel(t *testing.T) {
l1 := Line{P1: Point{1, 2}, P2: Point{2, 4}}
l2 := Line{P1: Point{25, 50}, P2: Point{50, 100}}
if !IsParallel(&l1, &l2) {
t.Fatalf("Failed to check if Parallel.")
}
}
func TestIsPerpendicular(t *testing.T) {
l1 := Line{P1: Point{1, 2}, P2: Point{2, 4}}
l2 := Line{P1: Point{2, 2}, P2: Point{4, 1}}
if !IsPerpendicular(&l1, &l2) {
t.Fatalf("Failed to check if Perpendicular.")
}
}
func TestPointDistance(t *testing.T) {
p := Point{1, 1}
equation := [3]float64{4, 3, 1}
var wantedDistance float64 = 1.6
var calculatedDistance float64 = PointDistance(&p, equation)
if calculatedDistance != wantedDistance {
t.Fatalf("Failed to calculate Point Distance.")
}
}
================================================
FILE: math/isautomorphic.go
================================================
// isautomorphic.go
// description: Checks whether a whole number integer is Automorphic or not. If number < 0 then returns false.
// details:
// In mathematics, a number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.
// ref: (https://en.wikipedia.org/wiki/Automorphic_number)
// time complexity: O(log10(N))
// space complexity: O(1)
// author: [SilverDragonOfR](https://github.com/SilverDragonOfR)
package math
import (
"github.com/TheAlgorithms/Go/constraints"
)
func IsAutomorphic[T constraints.Integer](n T) bool {
// handling the negetive number case
if n < 0 {
return false
}
n_sq := n * n
for n > 0 {
if (n % 10) != (n_sq % 10) {
return false
}
n /= 10
n_sq /= 10
}
return true
}
================================================
FILE: math/isautomorphic_test.go
================================================
package math
import (
"testing"
)
var testCases = []struct {
name string
input int
expected bool
}{
{
"negetive number: not Automorphic",
-1,
false,
},
{
"negetive number: not Automorphic",
-146,
false,
},
{
"0: is Automorphic",
0,
true,
},
{
"1: is Automorphic",
1,
true,
},
{
"7: not Automorphic",
7,
false,
},
{
"83: not Automorphic",
83,
false,
},
{
"376: is Automorphic",
376,
true,
},
}
func TestIsAutomorphic(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
funcResult := IsAutomorphic(test.input)
if test.expected != funcResult {
t.Errorf("Expected answer '%t' for the number '%d' but answer given was %t", test.expected, test.input, funcResult)
}
})
}
}
================================================
FILE: math/krishnamurthy.go
================================================
// filename : krishnamurthy.go
// description: A program which contains the function that returns true if a given number is Krishnamurthy number or not.
// details: A number is a Krishnamurthy number if the sum of all the factorials of the digits is equal to the number.
// Ex: 1! = 1, 145 = 1! + 4! + 5!
// time complexity: O(log n)
// space complexity: O(1)
// author(s): [GooMonk](https://github.com/GooMonk)
// see krishnamurthy_test.go
package math
import "github.com/TheAlgorithms/Go/constraints"
// IsKrishnamurthyNumber returns if the provided number n is a Krishnamurthy number or not.
func IsKrishnamurthyNumber[T constraints.Integer](n T) bool {
if n <= 0 {
return false
}
// Preprocessing: Using a slice to store the digit Factorials
digitFact := make([]T, 10)
digitFact[0] = 1 // 0! = 1
for i := 1; i < 10; i++ {
digitFact[i] = digitFact[i-1] * T(i)
}
// Subtract the digit Facotorial from the number
nTemp := n
for n > 0 {
nTemp -= digitFact[n%10]
n /= 10
}
return nTemp == 0
}
================================================
FILE: math/krishnamurthy_test.go
================================================
package math
import (
"fmt"
"testing"
)
func retCases() []struct {
input int64
output bool
outputString string
} {
return []struct {
input int64
output bool
outputString string
}{
{-3112312321, false, "is not"},
{0, false, "is not"},
{1, true, "is"},
{2, true, "is"},
{109, false, "is not"},
{145, true, "is"},
{943, false, "is not"},
{6327, false, "is not"},
{40585, true, "is"},
{9743821, false, "is not"},
{3421488712, false, "is not"},
}
}
func TestIsKrishnamurthyNumber(t *testing.T) {
for _, test := range retCases() {
t.Run(fmt.Sprintf("%d %s a Krishnamurthy Number", test.input, test.outputString), func(t *testing.T) {
res := IsKrishnamurthyNumber(test.input)
if res != test.output {
t.Errorf("for input %d, expected: %t, found: %t", test.input, test.output, res)
}
})
}
}
func BenchmarkIsKrishnamurthyNumber(b *testing.B) {
for _, test := range retCases() {
b.Run(fmt.Sprintf("%d %s a Krishnamurthy Number", test.input, test.outputString), func(b *testing.B) {
for i := 0; i < b.N; i++ {
IsKrishnamurthyNumber(test.input)
}
})
}
}
================================================
FILE: math/kthnumber.go
================================================
package math
import (
"github.com/TheAlgorithms/Go/search"
"github.com/TheAlgorithms/Go/sort"
)
// FindKthMax returns the kth large element given an integer slice
// with nil `error` if found and returns -1 with `error` `search.ErrNotFound`
// if not found. NOTE: The `nums` slice gets mutated in the process.
func FindKthMax(nums []int, k int) (int, error) {
index := len(nums) - k
return kthNumber(nums, index)
}
// FindKthMin returns kth small element given an integer slice
// with nil `error` if found and returns -1 with `error` `search.ErrNotFound`
// if not found. NOTE: The `nums` slice gets mutated in the process.
func FindKthMin(nums []int, k int) (int, error) {
index := k - 1
return kthNumber(nums, index)
}
// kthNumber use the selection algorithm (based on the partition method - the same one as used in quicksort).
func kthNumber(nums []int, k int) (int, error) {
if k < 0 || k >= len(nums) {
return -1, search.ErrNotFound
}
start := 0
end := len(nums) - 1
for start <= end {
pivot := sort.Partition(nums, start, end)
if k == pivot {
return nums[pivot], nil
}
if k > pivot {
start = pivot + 1
continue
}
end = pivot - 1
}
return -1, search.ErrNotFound
}
================================================
FILE: math/kthnumber_test.go
================================================
package math
import (
"github.com/TheAlgorithms/Go/search"
"testing"
)
func TestFindKthMax(t *testing.T) {
sortTests := []struct {
input []int
k int
expected int
err error
name string
}{
{
input: []int{6, 7, 0, -1, 10, 70, 8, 22, 3, 9},
k: 3,
expected: 10,
name: "3th largest number",
},
{
input: []int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
k: 3,
expected: -1,
name: "3th largest number",
},
{
input: []int{-1, -1, -1, -1, -1, -1},
k: 7,
expected: -1,
err: search.ErrNotFound,
name: "This should be an error",
},
{
input: []int{},
k: 1,
expected: -1,
err: search.ErrNotFound,
name: "This should be an error",
},
}
for _, test := range sortTests {
t.Run(test.name, func(t *testing.T) {
actual, err := FindKthMax(test.input, test.k)
if err != test.err {
t.Errorf("name:%v FindKthMax() = %v, want err: %v", test.name, err, test.err)
}
if actual != test.expected {
t.Errorf("test %s failed", test.name)
t.Errorf("actual %v expected %v", actual, test.expected)
}
})
}
}
func TestFindKthMin(t *testing.T) {
sortTests := []struct {
input []int
k int
expected int
err error
name string
}{
{
input: []int{6, 7, 0, -1, 10, 70, 8, 22, 3, 9},
k: 3,
expected: 3,
name: "3th smallest number",
},
{
input: []int{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
k: 3,
expected: -1,
name: "3th smallest number",
},
{
input: []int{-1, -1, -1, -1, -1, -1},
k: 7,
expected: -1,
err: search.ErrNotFound,
name: "This should be an error",
},
{
input: []int{},
k: 1,
expected: -1,
err: search.ErrNotFound,
name: "This should be an error",
},
}
for _, test := range sortTests {
t.Run(test.name, func(t *testing.T) {
actual, err := FindKthMin(test.input, test.k)
if err != test.err {
t.Errorf("name:%v FindKthMin() = %v, want err: %v", test.name, err, test.err)
}
if actual != test.expected {
t.Errorf("test %s failed", test.name)
t.Errorf("actual %v expected %v", actual, test.expected)
}
})
}
}
================================================
FILE: math/lcm/lcm.go
================================================
package lcm
import (
"math"
"github.com/TheAlgorithms/Go/math/gcd"
)
// Lcm returns the lcm of two numbers using the fact that lcm(a,b) * gcd(a,b) = | a * b |
func Lcm(a, b int64) int64 {
return int64(math.Abs(float64(a*b)) / float64(gcd.Iterative(a, b)))
}
================================================
FILE: math/lcm/lcm_test.go
================================================
package lcm
import "testing"
func TestLcm(t *testing.T) {
testCases := []struct {
name string
a int64
b int64
output int64
}{
{
name: "LCM of 1 & 5",
a: 1,
b: 5,
output: 5,
}, {
name: "LCM of 2 & 5",
a: 2,
b: 5,
output: 10,
}, {
name: "LCM of 5 & 10",
a: 10,
b: 5,
output: 10,
}, {
name: "LCM of 5 & 5",
a: 5,
b: 5,
output: 5,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual_output := Lcm(tc.a, tc.b)
if actual_output != tc.output {
t.Errorf("Expected LCM of %d and %d is %d, but got %d", tc.a, tc.b, tc.output,
actual_output)
}
})
}
}
================================================
FILE: math/lerp.go
================================================
package math
// Lerp or Linear interpolation
// This function will return new value in 't' percentage between 'v0' and 'v1'
func Lerp(v0, v1, t float64) float64 {
// see: https://en.wikipedia.org/wiki/Linear_interpolation
return (1-t)*v0 + t*v1
}
================================================
FILE: math/lerp_test.go
================================================
package math_test
import (
"testing"
algmath "github.com/TheAlgorithms/Go/math"
)
func TestLerp(t *testing.T) {
tests := []struct {
name string
testValues []float64
answer float64
}{
{"Lerp(1,1,1)", []float64{1, 1, 1}, 1},
{"Lerp(0,1,1)", []float64{0, 1, 1}, 1},
{"Lerp(0,1,0.5)", []float64{0, 1, 0.5}, 0.5},
{"Lerp(0,1,0.1)", []float64{0, 1, 0.1}, 0.1},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := algmath.Lerp(test.testValues[0], test.testValues[1], test.testValues[2])
if got != test.answer {
t.Errorf("Lerp(%f,%f,%f) = %v, want %v", got, test.testValues[0],
test.testValues[1], test.testValues[2], test.answer)
}
})
}
}
================================================
FILE: math/liouville.go
================================================
// liouville.go
// description: Returns λ(n)
// details:
// For any positive integer n, define λ(n) as the sum of the primitive nth roots of unity.
// It has values in {−1, 1} depending on the factorization of n into prime factors:
// λ(n) = +1 if n is a positive integer with an even number of prime factors.
// λ(n) = −1 if n is a positive integer with an odd number of prime factors.
// wikipedia: https://en.wikipedia.org/wiki/Liouville_function
// time complexity: O(log n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see liouville_test.go
package math
import (
"errors"
"github.com/TheAlgorithms/Go/math/prime"
)
var ErrNonZeroArgsOnly error = errors.New("arguments cannot be zero")
// Lambda is the liouville function
// This function returns λ(n) for given number
func LiouvilleLambda(n int) (int, error) {
switch {
case n < 0:
return 0, ErrPosArgsOnly
case n == 0:
return 0, ErrNonZeroArgsOnly
case len(prime.Factorize(int64(n)))%2 == 0:
return 1, nil
default:
return -1, nil
}
}
================================================
FILE: math/liouville_test.go
================================================
// liouville_test.go
// description: Returns λ(n)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see liouville.go
package math_test
import (
"testing"
"github.com/TheAlgorithms/Go/math"
)
func TestLiouvilleLambda(t *testing.T) {
var tests = []struct {
name string
n int
expectedValue int
expectedError error
}{
{"n = 10", 10, 1, nil},
{"n = 11", 11, -1, nil},
{"n = -1", -1, 0, math.ErrPosArgsOnly},
{"n = 0", 0, 0, math.ErrNonZeroArgsOnly},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := math.LiouvilleLambda(test.n)
if result != test.expectedValue || test.expectedError != err {
t.Errorf("expected error: %s, got: %s; expected value: %v, got: %v", test.expectedError, err, test.expectedValue, result)
}
})
}
}
func BenchmarkLiouvilleLambda(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = math.LiouvilleLambda(65536)
}
}
================================================
FILE: math/math_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package math
================================================
FILE: math/matrix/add.go
================================================
// add.go
// description: Add two matrices
// time complexity: O(n^2)
// space complexity: O(n^2)
package matrix
import (
"context"
"errors"
"sync"
)
// Add adds two matrices.
func (m1 Matrix[T]) Add(m2 Matrix[T]) (Matrix[T], error) {
// Check if the matrices have the same dimensions.
if !m1.MatchDimensions(m2) {
return Matrix[T]{}, errors.New("matrices are not compatible for addition")
}
// Create a new matrix to store the result.
var zeroVal T
result := New(m1.Rows(), m1.Columns(), zeroVal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Make sure it's called to release resources even if no errors
var wg sync.WaitGroup
errCh := make(chan error, 1)
for i := 0; i < m1.rows; i++ {
i := i // Capture the loop variable for the goroutine
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < m1.columns; j++ {
select {
case <-ctx.Done():
return // Context canceled; return without an error
default:
}
sum := m1.elements[i][j] + m2.elements[i][j]
err := result.Set(i, j, sum)
if err != nil {
cancel() // Cancel the context on error
select {
case errCh <- err:
default:
}
return
}
}
}()
}
// Wait for all goroutines to finish
go func() {
wg.Wait()
close(errCh)
}()
// Check for any errors
if err := <-errCh; err != nil {
return Matrix[T]{}, err
}
return result, nil
}
================================================
FILE: math/matrix/add_test.go
================================================
package matrix_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestAdd(t *testing.T) {
// Create two matrices with the same dimensions for addition
m1 := matrix.New(2, 2, 1)
m2 := matrix.New(2, 2, 2)
// Test case 1: Valid matrix addition
addedMatrix, err := m1.Add(m2)
if err != nil {
t.Errorf("Add(m1, m2) returned an error: %v, expected no error", err)
}
expectedMatrix := matrix.New(2, 2, 3)
res := addedMatrix.CheckEqual(expectedMatrix)
if !res {
t.Errorf("Add(m1, m2) returned incorrect result:\n%v\nExpected:\n%v", addedMatrix, expectedMatrix)
}
// Create two matrices with different dimensions for addition
m3 := matrix.New(2, 2, 1)
m4 := matrix.New(2, 3, 2)
// Test case 2: Matrices with different dimensions
_, err2 := m3.Add(m4)
expectedError2 := fmt.Errorf("matrices are not compatible for addition")
if err2 == nil || err2.Error() != expectedError2.Error() {
t.Errorf("Add(m3, m4) returned error: %v, expected error: %v", err2, expectedError2)
}
}
func BenchmarkAddSmallMatrix(b *testing.B) {
m1 := matrix.New(10, 10, 0) // Create a 10x10 matrix with all zeros
m2 := matrix.New(10, 10, 1) // Create a 10x10 matrix with all ones
for i := 0; i < b.N; i++ {
_, _ = m1.Add(m2)
}
}
func BenchmarkAddLargeMatrix(b *testing.B) {
size := 1000 // Choose an appropriate size for your large matrix
m1 := MakeRandomMatrix[int](size, size)
m2 := MakeRandomMatrix[int](size, size)
b.ResetTimer() // Reset the timer to exclude setup time
for i := 0; i < b.N; i++ {
_, _ = m1.Add(m2)
}
}
================================================
FILE: math/matrix/checkequal.go
================================================
package matrix
// CheckEqual checks if the current matrix is equal to another matrix (m2).
// Two matrices are considered equal if they have the same dimensions and
// all their elements are equal.
// time complexity: O(n*m) where n and m are the dimensions of the matrix
// space complexity: O(1)
func (m1 Matrix[T]) CheckEqual(m2 Matrix[T]) bool {
if !m1.MatchDimensions(m2) {
return false
}
c := make(chan bool)
for i := range m1.elements {
go func(i int) {
for j := range m1.elements[i] {
if m1.elements[i][j] != m2.elements[i][j] {
c <- false
return
}
}
c <- true
}(i)
}
for range m1.elements {
if !<-c {
return false
}
}
return true
}
================================================
FILE: math/matrix/checkequal_test.go
================================================
package matrix_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestCheckEqual(t *testing.T) {
// Create two matrices with the same dimensions and equal values
m1 := matrix.New(2, 2, 0)
m2 := matrix.New(2, 2, 0)
// Test case 1: Matrices are equal
equal := m1.CheckEqual(m2)
if !equal {
t.Errorf("CheckEqual(m1, m2) returned false, expected true (matrices are equal)")
}
// Create two matrices with the same dimensions but different values
m3 := matrix.New(2, 2, 1)
m4 := matrix.New(2, 2, 0)
// Test case 2: Matrices are not equal
equal2 := m3.CheckEqual(m4)
if equal2 {
t.Errorf("CheckEqual(m3, m4) returned true, expected false (matrices are not equal)")
}
// Create two matrices with different dimensions
m5 := matrix.New(2, 2, 0)
m6 := matrix.New(2, 3, 0)
// Test case 3: Matrices have different dimensions
equal3 := m5.CheckEqual(m6)
if equal3 {
t.Errorf("CheckEqual(m5, m6) returned true, expected false (matrices are not equal)")
}
}
func BenchmarkCheckEqualSmallMatrix(b *testing.B) {
m1 := matrix.New(10, 10, 0) // Create a 10x10 matrix with all zeros
m2 := matrix.New(10, 10, 0) // Create another 10x10 matrix with all zeros
for i := 0; i < b.N; i++ {
_ = m1.CheckEqual(m2)
}
}
func BenchmarkCheckEqualLargeMatrix(b *testing.B) {
size := 1000 // Choose an appropriate size for your large matrix
m1 := MakeRandomMatrix[int](size, size)
m2 := MakeRandomMatrix[int](size, size)
b.ResetTimer() // Reset the timer to exclude setup time
for i := 0; i < b.N; i++ {
_ = m1.CheckEqual(m2)
}
}
================================================
FILE: math/matrix/copy.go
================================================
// copy.go
// description: Copy a matrix
// details: This function creates a new matrix with the same dimensions as the original matrix and copies all the elements from the original matrix to the new matrix.
// time complexity: O(n*m) where n and m are the dimensions of the matrix
// space complexity: O(n*m) where n and m are the dimensions of the matrix
package matrix
import "sync"
func (m Matrix[T]) Copy() (Matrix[T], error) {
rows := m.Rows()
columns := m.Columns()
if rows == 0 || columns == 0 {
return Matrix[T]{}, nil
}
zeroVal, err := m.Get(0, 0) // Get the zero value of the element type
if err != nil {
return Matrix[T]{}, err
}
copyMatrix := New(rows, columns, zeroVal)
var wg sync.WaitGroup
wg.Add(rows)
errChan := make(chan error, 1)
for i := 0; i < rows; i++ {
go func(i int) {
defer wg.Done()
for j := 0; j < columns; j++ {
val, err := m.Get(i, j)
if err != nil {
select {
case errChan <- err:
default:
}
return
}
err = copyMatrix.Set(i, j, val)
if err != nil {
select {
case errChan <- err:
default:
}
return
}
}
}(i)
}
wg.Wait()
close(errChan)
if err, ok := <-errChan; ok {
return Matrix[T]{}, err
}
return copyMatrix, nil
}
================================================
FILE: math/matrix/copy_test.go
================================================
package matrix_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestMatrixCopy(t *testing.T) {
// Create a sample matrix
data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
// Ensure that the copy is not the same as the original
matrix, err := matrix.NewFromElements(data)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
copyMatrix, err := matrix.Copy()
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
// Ensure that the copy is not the same as the original
if &matrix == ©Matrix {
t.Errorf("Copy did not create a new matrix.")
}
for i := 0; i < matrix.Rows(); i++ {
for j := 0; j < matrix.Columns(); j++ {
val1, err := matrix.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
val2, err := copyMatrix.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
if val1 != val2 {
t.Errorf("Copy did not correctly copy element (%d, %d).", i, j)
}
}
}
}
func TestMatrixCopyEmpty(t *testing.T) {
// Create an empty matrix
emptyMatrix := matrix.New(0, 0, 0)
// Make a copy of the empty matrix
copyMatrix, err := emptyMatrix.Copy()
if err != nil { // as empty matrix
t.Fatalf("Failed to copy matrix: %v", err)
}
// Ensure that the copy is not the same as the original by comparing their addresses
if &emptyMatrix == ©Matrix {
t.Errorf("Copy did not create a new matrix for an empty matrix.")
}
// Check if the copy is also empty
if copyMatrix.Rows() != 0 || copyMatrix.Columns() != 0 {
t.Errorf("Copy of an empty matrix should also be empty.")
}
}
func TestMatrixCopyWithDefaultValues(t *testing.T) {
// Create a matrix with default values (zeroes)
rows, columns := 3, 3
defaultValue := 0
defaultMatrix := matrix.New(rows, columns, defaultValue)
// Make a copy of the matrix
copyMatrix, err := defaultMatrix.Copy()
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
// Ensure that the copy is not the same as the original by comparing their addresses
if &defaultMatrix == ©Matrix {
t.Errorf("Copy did not create a new matrix for default values.")
}
// Check if the copy has the same values as the original (all zeroes)
for i := 0; i < defaultMatrix.Rows(); i++ {
for j := 0; j < defaultMatrix.Columns(); j++ {
val1, err := defaultMatrix.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
val2, err := copyMatrix.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
if val1 != val2 || val1 != defaultValue || val2 != defaultValue {
t.Errorf("Copy did not preserve default values at row %d, column %d. Expected %v, got %v", i, j, defaultValue, val2)
}
}
}
}
func BenchmarkCopyMatrix(b *testing.B) {
// Create a matrix for benchmarking
rows := 100
columns := 100
initialValue := 0
matrix := matrix.New(rows, columns, initialValue)
// Reset the benchmark timer
b.ResetTimer()
// Run the benchmarks
for i := 0; i < b.N; i++ {
_, _ = matrix.Copy()
}
}
================================================
FILE: math/matrix/determinant.go
================================================
// determinant.go
// description: This method finds the determinant of a matrix.
// details: For a theoretical explanation as for what the determinant
// represents, see the [Wikipedia Article](https://en.wikipedia.org/wiki/Determinant)
// time complexity: O(n!) where n is the number of rows and columns in the matrix.
// space complexity: O(n^2) where n is the number of rows and columns in the matrix.
// author [Carter907](https://github.com/Carter907)
// see determinant_test.go
package matrix
import (
"errors"
)
// Calculates the determinant of the matrix.
// This method only works for square matrices (e.i. matrices with equal rows and columns).
func (mat Matrix[T]) Determinant() (T, error) {
var determinant T = 0
var elements = mat.elements
if mat.rows != mat.columns {
return 0, errors.New("Matrix rows and columns must equal in order to find the determinant.")
}
// Specify base cases for different sized matrices.
switch mat.rows {
case 1:
return elements[0][0], nil
case 2:
return elements[0][0]*elements[1][1] - elements[1][0]*elements[0][1], nil
default:
for i := 0; i < mat.rows; i++ {
var initialValue T = 0
minor := New(mat.rows-1, mat.columns-1, initialValue)
// Fill the contents of minor excluding the 0th row and the ith column.
for j, minor_i := 1, 0; j < mat.rows && minor_i < minor.rows; j, minor_i = j+1, minor_i+1 {
for k, minor_j := 0, 0; k < mat.rows && minor_j < minor.rows; k, minor_j = k+1, minor_j+1 {
if k != i {
minor.elements[minor_i][minor_j] = elements[j][k]
} else {
minor_j-- // Decrement the column of minor to account for skipping the ith column of the matrix.
}
}
}
if i%2 == 0 {
minor_det, _ := minor.Determinant()
determinant += elements[0][i] * minor_det
} else {
minor_det, _ := minor.Determinant()
determinant += elements[0][i] * minor_det
}
}
return determinant, nil
}
}
================================================
FILE: math/matrix/determinant_test.go
================================================
package matrix_test
import (
"errors"
"math"
"math/rand"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
// Test different matrix contents
func TestMatrixDeterminant(t *testing.T) {
// Find Determinant of a 2 by 2 matrix.
matrix1, err := matrix.NewFromElements([][]int{
{3, 8},
{4, 6},
})
if err != nil {
t.Fatalf("Error creating 3 by 3 matrix: %v", err)
}
determinant, err := matrix1.Determinant()
if err != nil {
t.Fatalf("Error returned from 3 by 3 matrix: %v", err)
}
if determinant != -14 {
t.Fatalf("Determinant returned for a 3 by 3 matrix was %d; wanted -14", determinant)
}
// Find Dertminant of a 1 by 1 matrix
expectedValue := rand.Intn(math.MaxInt)
matrix2, err := matrix.NewFromElements([][]int{
{expectedValue},
})
if err != nil {
t.Fatalf("Error creating 1 by 1 matrix: %v", err)
}
determinant, err = matrix2.Determinant()
if err != nil {
t.Fatalf("Error returned from 1 by 1 matrix: %v", err)
}
if determinant != expectedValue {
t.Fatalf("Determinant returned for a 1 by 1 matrix was %d; wanted %d", determinant, expectedValue)
}
}
func TestEmptyMatrix(t *testing.T) {
emptyElements := [][]int{}
matrix, err := matrix.NewFromElements(emptyElements)
if err != nil {
t.Fatalf("Error creating Matrix with empty elements: %v", err)
}
determinant, err := matrix.Determinant()
if err != nil {
t.Fatalf("Determinant returned an error for empty matrix: %v", err)
}
// Check that 0 is returned from an empty matrix.
expectedValue := 0
if determinant != expectedValue {
t.Errorf("Determinant returned from empty matrix was %d; wanted %d", determinant, expectedValue)
}
}
func TestNonSquareMatrix(t *testing.T) {
// Creating non-square matrix for testing.
initialValue := 0
initialRows := 4
initialCols := 2
nonSquareMatrix := matrix.New(initialRows, initialCols, initialValue)
determinant, err := nonSquareMatrix.Determinant()
// Check if non square matrix returns an error.
if err == nil {
t.Fatalf("No error was returned for a non-square matrix")
}
// Check if the correct error was returned.
expectedError := errors.New("Matrix rows and columns must equal in order to find the determinant.")
if err.Error() != expectedError.Error() {
t.Errorf("Error returned from non-square matrix was \n\"%v\"; \nwanted \n\"%v\"", err, expectedError)
}
// Check if the determinant of the non-square matrix is 0.
if determinant != 0 {
t.Errorf("Determinant of non-square matrix was not 0 but was %d", determinant)
}
}
// Test matrix returned from matrix.New
func TestDefaultMatrix(t *testing.T) {
initialValue := 0
initialRows := 3
initialCols := 3
defaultMatrix := matrix.New(initialRows, initialCols, initialValue)
determinant, err := defaultMatrix.Determinant()
if err != nil {
t.Fatalf("Error finding the determinant of 3 by 3 default matrix: %v.", err)
}
expectedValue := 0
if determinant != expectedValue {
t.Errorf("Determinant of the default matrix with an initial value 0 was %d; wanted %d.", initialValue, expectedValue)
}
}
// Benchmark a 3 by 3 matrix for computational throughput
func BenchmarkSmallMatrixDeterminant(b *testing.B) {
// Create a 3 by 3 matrix for benchmarking
rows := 3
columns := 3
initialValue := 0
matrix := matrix.New(rows, columns, initialValue)
for i := 0; i < b.N; i++ {
_, _ = matrix.Determinant()
}
}
// Benchmark a 10 by 10 matrix for computational throughput.
func BenchmarkMatrixDeterminant(b *testing.B) {
// Create a 10 by 10 matrix for benchmarking
rows := 10
columns := 10
initialValue := 0
matrix := matrix.New(rows, columns, initialValue)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = matrix.Determinant()
}
}
================================================
FILE: math/matrix/isvalid.go
================================================
package matrix
import "github.com/TheAlgorithms/Go/constraints"
// IsValid checks if the input matrix has consistent row lengths.
func IsValid[T constraints.Integer](elements [][]T) bool {
if len(elements) == 0 {
return true
}
columns := len(elements[0])
for _, row := range elements {
if len(row) != columns {
return false
}
}
return true
}
================================================
FILE: math/matrix/isvalid_test.go
================================================
package matrix_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestIsValid(t *testing.T) {
// Test case 1: Valid matrix with consistent row lengths
validMatrix := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
result1 := matrix.IsValid(validMatrix)
if !result1 {
t.Errorf("IsValid(validMatrix) returned false, expected true (valid matrix)")
}
// Test case 2: Valid matrix with empty rows (no inconsistency)
validMatrixEmptyRows := [][]int{
{},
{},
{},
}
result2 := matrix.IsValid(validMatrixEmptyRows)
if !result2 {
t.Errorf("IsValid(validMatrixEmptyRows) returned false, expected true (valid matrix with empty rows)")
}
// Test case 3: Invalid matrix with inconsistent row lengths
invalidMatrix := [][]int{
{1, 2, 3},
{4, 5},
{6, 7, 8},
}
result3 := matrix.IsValid(invalidMatrix)
if result3 {
t.Errorf("IsValid(invalidMatrix) returned true, expected false (invalid matrix with inconsistent row lengths)")
}
}
func BenchmarkIsValid(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
elements := make([][]int, rows)
for i := range elements {
elements[i] = make([]int, columns)
for j := range elements[i] {
elements[i][j] = i*columns + j // Some arbitrary values
}
}
// Reset the benchmark timer
b.ResetTimer()
// Run the benchmark
for i := 0; i < b.N; i++ {
_ = matrix.IsValid(elements)
}
}
================================================
FILE: math/matrix/matchdimensions.go
================================================
package matrix
// MatchDimensions checks if two matrices have the same dimensions.
func (m Matrix[T]) MatchDimensions(m1 Matrix[T]) bool {
if m.rows == m1.rows && m.columns == m1.columns {
return true
}
return false
}
================================================
FILE: math/matrix/matchdimensions_test.go
================================================
package matrix_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestMatrixMatchDimensions(t *testing.T) {
// Create two matrices with the same dimensions
m1 := matrix.New(2, 3, 0)
m2 := matrix.New(2, 3, 0)
// Test case 1: Same dimensions
if !m1.MatchDimensions(m2) {
t.Errorf("m1.MatchDimensions(m2) returned %t, expected 1 (same dimensions)", m1.MatchDimensions(m2))
}
// Create two matrices with different dimensions
m3 := matrix.New(2, 3, 0)
m4 := matrix.New(3, 2, 0)
// Test case 2: Different dimensions
if m3.MatchDimensions(m4) {
t.Errorf("m3.MatchDimensions(m4) returned : %v, expected: %v", m3.MatchDimensions(m4), false)
}
}
// BenchmarkMatchDimensions benchmarks the MatchDimensions method.
func BenchmarkMatchDimensions(b *testing.B) {
// Create sample matrices for benchmarking
rows := 100
columns := 100
m1 := matrix.New(rows, columns, 0) // Replace with appropriate values
m2 := matrix.New(rows, columns, 0) // Replace with appropriate values
for i := 0; i < b.N; i++ {
_ = m1.MatchDimensions(m2)
}
}
================================================
FILE: math/matrix/matrix.go
================================================
package matrix
import (
"errors"
"sync"
"github.com/TheAlgorithms/Go/constraints"
)
type Matrix[T constraints.Integer] struct {
elements [][]T
rows int
columns int
}
// NewMatrix creates a new Matrix based on the provided arguments.
func New[T constraints.Integer](rows, columns int, initial T) Matrix[T] {
if rows < 0 || columns < 0 {
return Matrix[T]{} // Invalid dimensions, return an empty matrix
}
// Initialize the matrix with the specified dimensions and fill it with the initial value.
elements := make([][]T, rows)
var wg sync.WaitGroup
wg.Add(rows)
for i := range elements {
go func(i int) {
defer wg.Done()
elements[i] = make([]T, columns)
for j := range elements[i] {
elements[i][j] = initial
}
}(i)
}
wg.Wait()
return Matrix[T]{elements, rows, columns}
}
// NewFromElements creates a new Matrix from the given elements.
func NewFromElements[T constraints.Integer](elements [][]T) (Matrix[T], error) {
if !IsValid(elements) {
return Matrix[T]{}, errors.New("rows have different numbers of columns")
}
rows := len(elements)
if rows == 0 {
return Matrix[T]{}, nil // Empty matrix
}
columns := len(elements[0])
matrix := Matrix[T]{
elements: make([][]T, rows),
rows: rows, // Set the rows field
columns: columns, // Set the columns field
}
for i := range matrix.elements {
matrix.elements[i] = make([]T, columns)
copy(matrix.elements[i], elements[i])
}
return matrix, nil
}
func (m Matrix[T]) Get(row, col int) (T, error) {
if row < 0 || row >= m.rows || col < 0 || col >= m.columns {
var zeroVal T
return zeroVal, errors.New("index out of range")
}
return m.elements[row][col], nil
}
func (m Matrix[T]) Set(row, col int, val T) error {
if row < 0 || row >= m.rows || col < 0 || col >= m.columns {
return errors.New("index out of bounds")
}
m.elements[row][col] = val
return nil
}
func (m Matrix[T]) Rows() int {
return len(m.elements)
}
func (m Matrix[T]) Columns() int {
if len(m.elements) == 0 {
return 0
}
return len(m.elements[0])
}
================================================
FILE: math/matrix/matrix_test.go
================================================
package matrix_test
import (
"errors"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestNewMatrix(t *testing.T) {
nullMatrix := matrix.New(0, 0, 0)
if nullMatrix.Rows() != 0 || nullMatrix.Columns() != 0 {
t.Errorf("matrix.New( 0, 0, 0) returned nil, expected a matrix")
}
// Test creating a matrix of integers
intMatrix := matrix.New(3, 4, 0)
if intMatrix.Rows() != 3 || intMatrix.Columns() != 4 {
t.Errorf("matrix.New( 3, 4, 0) returned nil, expected a matrix")
}
}
func TestNewFromElements(t *testing.T) {
// Test case 1: Valid matrix
validElements := [][]int{
{1, 2, 3},
{4, 5, 6},
}
expectedm1 := matrix.New(2, 3, 0)
for i := 0; i < len(validElements); i++ {
for j := 0; j < len(validElements[0]); j++ {
err := expectedm1.Set(i, j, validElements[i][j])
if err != nil {
t.Errorf("copyMatrix.Set error: %s", err.Error())
}
}
}
m1, err1 := matrix.NewFromElements(validElements)
if err1 != nil {
t.Errorf("NewFromElements(validElements) returned an error: %v", err1)
}
res := m1.CheckEqual(expectedm1)
if res != true {
t.Errorf("NewFromElements(validElements) returned %v, expected %v", m1, expectedm1)
}
// Test case 2: Invalid matrix with different column counts
invalidElements := [][]int{
{1, 2, 3},
{4, 5},
}
_, err2 := matrix.NewFromElements(invalidElements)
expectedError2 := errors.New("rows have different numbers of columns")
if err2 == nil || err2.Error() != expectedError2.Error() {
t.Errorf("NewFromElements(invalidElements) returned error: %v, expected error: %v", err2, expectedError2)
}
// Test case 3: Empty matrix
emptyElements := [][]int{}
m3, err3 := matrix.NewFromElements(emptyElements)
if err3 != nil {
t.Errorf("NewFromElements(emptyElements) returned an error: %v", err3)
}
if m3.Rows() != 0 || m3.Columns() != 0 {
t.Errorf("NewFromElements(emptyElements) returned %v, expected nil", m3)
}
}
func TestMatrixGet(t *testing.T) {
// Create a sample matrix for testing
matrix := matrix.New(3, 3, 0)
err := matrix.Set(1, 1, 42) // Set a specific value for testing
if err != nil {
t.Errorf("copyMatrix.Set error: %s", err.Error())
}
// Test case 1: Valid Get
val1, err1 := matrix.Get(1, 1)
if err1 != nil {
t.Errorf("matrix.Get(1, 1) returned an error: %v, expected no error", err1)
}
if val1 != 42 {
t.Errorf("matrix.Get(1, 1) returned %v, expected 42", val1)
}
// Test case 2: Get with invalid indices
_, err2 := matrix.Get(10, 10)
expectedError2 := errors.New("index out of range")
if err2 == nil || err2.Error() != expectedError2.Error() {
t.Errorf("matrix.Get(10, 10) returned error: %v, expected error: %v", err2, expectedError2)
}
// Test case 3: Get with invalid indices
_, err3 := matrix.Get(-1, -3)
expectedError3 := errors.New("index out of range")
if err3 == nil || err3.Error() != expectedError3.Error() {
t.Errorf("matrix.Get(10, 10) returned error: %v, expected error: %v", err3, expectedError3)
}
}
func TestMatrixSet(t *testing.T) {
// Create a sample matrix for testing
matrix := matrix.New(3, 3, 0)
// Test case 1: Valid Set
err1 := matrix.Set(1, 1, 42)
if err1 != nil {
t.Errorf("matrix.Set(1, 1, 42) returned an error: %v, expected no error", err1)
}
val1, err := matrix.Get(1, 1)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
if val1 != 42 {
t.Errorf("matrix.Set(1, 1, 42) did not set the value correctly, expected 42, got %v", val1)
}
// Test case 2: Set with invalid indices
err2 := matrix.Set(10, 10, 100)
expectedError2 := errors.New("index out of bounds")
if err2 == nil || err2.Error() != expectedError2.Error() {
t.Errorf("matrix.Set(10, 10, 100) returned error: %v, expected error: %v", err2, expectedError2)
}
// Test case 3: Get with invalid indices
err3 := matrix.Set(-13, -1, 100)
expectedError3 := errors.New("index out of bounds")
if err3 == nil || err3.Error() != expectedError3.Error() {
t.Errorf("matrix.Get(10, 10) returned error: %v, expected error: %v", err3, expectedError3)
}
}
func TestMatrixRows(t *testing.T) {
// Create a sample matrix
data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
matrix, err := matrix.NewFromElements(data)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
// Check the number of rows
expectedRows := len(data)
rows := matrix.Rows()
if rows != expectedRows {
t.Errorf("Expected %d rows, but got %d", expectedRows, rows)
}
}
func TestMatrixColumns(t *testing.T) {
// Create a sample matrix
data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
matrix, err := matrix.NewFromElements(data)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
// Check the number of columns
expectedColumns := len(data[0])
columns := matrix.Columns()
if columns != expectedColumns {
t.Errorf("Expected %d columns, but got %d", expectedColumns, columns)
}
}
func TestMatrixEmptyRowsAndColumns(t *testing.T) {
// Create an empty matrix
emptyMatrix := matrix.New(0, 0, 0)
// Check the number of rows and columns for an empty matrix
rows := emptyMatrix.Rows()
columns := emptyMatrix.Columns()
if rows != 0 {
t.Errorf("Expected 0 rows for an empty matrix, but got %d", rows)
}
if columns != 0 {
t.Errorf("Expected 0 columns for an empty matrix, but got %d", columns)
}
}
// BenchmarkNew benchmarks the New function.
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = matrix.New(100, 100, 0) // Change the arguments to match your use case
}
}
// BenchmarkNewFromElements benchmarks the NewFromElements function.
func BenchmarkNewFromElements(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
elements := make([][]int, rows)
for i := range elements {
elements[i] = make([]int, columns)
for j := range elements[i] {
elements[i][j] = i*columns + j // Some arbitrary values
}
}
for i := 0; i < b.N; i++ {
_, _ = matrix.NewFromElements(elements)
}
}
// BenchmarkGet benchmarks the Get method.
func BenchmarkGet(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
matrix := matrix.New(rows, columns, 0)
for i := 0; i < b.N; i++ {
_, _ = matrix.Get(50, 50) // Change the row and column indices as needed
}
}
// BenchmarkSet benchmarks the Set method.
func BenchmarkSet(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
matrix := matrix.New(rows, columns, 0)
for i := 0; i < b.N; i++ {
_ = matrix.Set(50, 50, 42) // Change the row, column, and value as needed
}
}
// BenchmarkRows benchmarks the Rows method.
func BenchmarkRows(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
matrix := matrix.New(rows, columns, 0)
for i := 0; i < b.N; i++ {
_ = matrix.Rows()
}
}
// BenchmarkColumns benchmarks the Columns method.
func BenchmarkColumns(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
matrix := matrix.New(rows, columns, 0)
for i := 0; i < b.N; i++ {
_ = matrix.Columns()
}
}
================================================
FILE: math/matrix/multiply.go
================================================
// multiply.go
// description: Implementation of matrix multiplication
// time complexity: O(n^3) where n is the number of rows in the first matrix
// space complexity: O(n^2) where n is the number of rows in the first matrix
package matrix
import (
"context"
"errors"
"sync"
)
// Multiply multiplies the current matrix (m1) with another matrix (m2) and returns the result as a new matrix.
func (m1 Matrix[T]) Multiply(m2 Matrix[T]) (Matrix[T], error) {
// Check if the matrices can be multiplied.
if m1.Columns() != m2.Rows() {
return Matrix[T]{}, errors.New("matrices cannot be multiplied: column count of the first matrix must match row count of the second matrix")
}
// Create a new matrix to store the result.
var zeroVal T
result := New(m1.Rows(), m2.Columns(), zeroVal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Make sure it's called to release resources even if no errors
var wg sync.WaitGroup
errCh := make(chan error, 1)
for i := 0; i < m1.Rows(); i++ {
for j := 0; j < m2.Columns(); j++ {
i, j := i, j // Capture the loop variable for the goroutine
wg.Add(1)
go func() {
defer wg.Done()
// Compute the dot product of the row from the first matrix and the column from the second matrix.
dotProduct := zeroVal
for k := 0; k < m1.Columns(); k++ {
select {
case <-ctx.Done():
return // Context canceled; return without an error
default:
}
val1, err := m1.Get(i, k)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
val2, err := m2.Get(k, j)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
dotProduct += val1 * val2
}
err := result.Set(i, j, dotProduct)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
}()
}
}
// Wait for all goroutines to finish
go func() {
wg.Wait()
close(errCh)
}()
// Check for any errors
if err := <-errCh; err != nil {
return Matrix[T]{}, err
}
return result, nil
}
================================================
FILE: math/matrix/multiply_test.go
================================================
package matrix_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestMultiplyMatrix(t *testing.T) {
// Test case with compatible NULL matrices
t.Run("NULL Matrices", func(t *testing.T) {
mat1 := matrix.New(0, 0, 0)
mat2 := matrix.New(0, 0, 0)
expected := matrix.New(0, 0, 0)
result, err := mat1.Multiply(mat2)
if err != nil {
t.Errorf("Expected no error, got %v", err)
} else if !result.CheckEqual(expected) {
t.Errorf("Result matrix does not match the expected result.")
}
})
// Test case with compatible matrices
t.Run("Compatible Matrices", func(t *testing.T) {
mat1 := [][]int{{1, 2, 3}, {4, 5, 6}}
mat2 := [][]int{{7, 8}, {9, 10}, {11, 12}}
m1, err := matrix.NewFromElements(mat1)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
m2, err := matrix.NewFromElements(mat2)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
exp := [][]int{{58, 64}, {139, 154}}
expected, err := matrix.NewFromElements(exp)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
result, err := m1.Multiply(m2)
if err != nil {
t.Errorf("Expected no error, got %v", err)
} else if !result.CheckEqual(expected) {
t.Errorf("Result matrix does not match the expected result.")
}
})
}
func TestMultiplyIncompatibleMatrix(t *testing.T) {
// Test case with incompatible matrices
t.Run("Incompatible Matrices", func(t *testing.T) {
mat1 := [][]int{{1, 2, 3}, {4, 5, 6}}
mat2 := [][]int{{7, 8}, {9, 10}}
m1, err := matrix.NewFromElements(mat1)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
m2, err := matrix.NewFromElements(mat2)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
_, err = m1.Multiply(m2)
if err == nil {
t.Error("Expected an error, but got none")
}
})
t.Run("Incompatible Matrices", func(t *testing.T) {
mat1 := [][]int{{1, 2}}
mat2 := [][]int{{}}
m1, err := matrix.NewFromElements(mat1)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
m2, err := matrix.NewFromElements(mat2)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
_, err = m1.Multiply(m2)
if err == nil {
t.Error("Expected an error, but got none")
}
})
}
func BenchmarkMatrixMultiply(b *testing.B) {
// Create sample matrices for benchmarking
rows := 10
columns := 10
m1 := matrix.New(rows, columns, 2) // Replace with appropriate values
m2 := matrix.New(rows, columns, 3) // Replace with appropriate values
for i := 0; i < b.N; i++ {
_, _ = m1.Multiply(m2)
}
}
================================================
FILE: math/matrix/strassenmatrixmultiply.go
================================================
// filename: strassenmatrixmultiply.go
// description: Implements matrix multiplication using the Strassen algorithm.
// details:
// This program takes two matrices as input and performs matrix multiplication
// using the Strassen algorithm, which is an optimized divide-and-conquer
// approach. It allows for efficient multiplication of large matrices.
// time complexity: O(n^2.81)
// space complexity: O(n^2)
// author(s): Mohit Raghav(https://github.com/mohit07raghav19)
// See strassenmatrixmultiply_test.go for test cases
package matrix
// Perform matrix multiplication using Strassen's algorithm
func (A Matrix[T]) StrassenMatrixMultiply(B Matrix[T]) (Matrix[T], error) {
n := A.rows
// Check if matrices are 2x2 or smaller
if n == 1 {
a1, err := A.Get(0, 0)
if err != nil {
return Matrix[T]{}, err
}
b1, err := B.Get(0, 0)
if err != nil {
return Matrix[T]{}, err
}
result := New(1, 1, a1*b1)
return result, nil
} else {
// Calculate the size of submatrices
mid := n / 2
// Create submatrices
A11, err := A.SubMatrix(0, 0, mid, mid)
if err != nil {
return Matrix[T]{}, err
}
A12, err := A.SubMatrix(0, mid, mid, n-mid)
if err != nil {
return Matrix[T]{}, err
}
A21, err := A.SubMatrix(mid, 0, n-mid, mid)
if err != nil {
return Matrix[T]{}, err
}
A22, err := A.SubMatrix(mid, mid, n-mid, n-mid)
if err != nil {
return Matrix[T]{}, err
}
B11, err := B.SubMatrix(0, 0, mid, mid)
if err != nil {
return Matrix[T]{}, err
}
B12, err := B.SubMatrix(0, mid, mid, n-mid)
if err != nil {
return Matrix[T]{}, err
}
B21, err := B.SubMatrix(mid, 0, n-mid, mid)
if err != nil {
return Matrix[T]{}, err
}
B22, err := B.SubMatrix(mid, mid, n-mid, n-mid)
if err != nil {
return Matrix[T]{}, err
}
// Calculate result submatrices
A1, err := A11.Add(A22)
if err != nil {
return Matrix[T]{}, err
}
A2, err := B11.Add(B22)
if err != nil {
return Matrix[T]{}, err
}
A3, err := A21.Add(A22)
if err != nil {
return Matrix[T]{}, err
}
A4, err := A11.Add(A12)
if err != nil {
return Matrix[T]{}, err
}
A5, err := B11.Add(B12)
if err != nil {
return Matrix[T]{}, err
}
A6, err := B21.Add(B22)
if err != nil {
return Matrix[T]{}, err
}
//
S1, err := B12.Subtract(B22)
if err != nil {
return Matrix[T]{}, err
}
S2, err := B21.Subtract(B11)
if err != nil {
return Matrix[T]{}, err
}
S3, err := A21.Subtract(A11)
if err != nil {
return Matrix[T]{}, err
}
S4, err := A12.Subtract(A22)
if err != nil {
return Matrix[T]{}, err
}
// Recursive steps
M1, err := A1.StrassenMatrixMultiply(A2)
if err != nil {
return Matrix[T]{}, err
}
M2, err := A3.StrassenMatrixMultiply(B11)
if err != nil {
return Matrix[T]{}, err
}
M3, err := A11.StrassenMatrixMultiply(S1)
if err != nil {
return Matrix[T]{}, err
}
M4, err := A22.StrassenMatrixMultiply(S2)
if err != nil {
return Matrix[T]{}, err
}
M5, err := A4.StrassenMatrixMultiply(B22)
if err != nil {
return Matrix[T]{}, err
}
M6, err := S3.StrassenMatrixMultiply(A5)
if err != nil {
return Matrix[T]{}, err
}
M7, err := S4.StrassenMatrixMultiply(A6)
if err != nil {
return Matrix[T]{}, err
} //
A7, err := M1.Add(M4)
if err != nil {
return Matrix[T]{}, err
}
A8, err := A7.Add(M7)
if err != nil {
return Matrix[T]{}, err
}
A9, err := M1.Add(M3)
if err != nil {
return Matrix[T]{}, err
}
A10, err := A9.Add(M6)
if err != nil {
return Matrix[T]{}, err
}
// Calculate result submatrices
C11, err := A8.Subtract(M5)
if err != nil {
return Matrix[T]{}, err
}
C12, err := M3.Add(M5)
if err != nil {
return Matrix[T]{}, err
}
C21, err := M2.Add(M4)
if err != nil {
return Matrix[T]{}, err
}
C22, err := A10.Subtract(M2)
if err != nil {
return Matrix[T]{}, err
}
// Combine subMatrices into the result matrix
var zeroVal T
C := New(n, n, zeroVal)
for i := 0; i < mid; i++ {
for j := 0; j < mid; j++ {
val, err := C11.Get(i, j)
if err != nil {
return Matrix[T]{}, err
}
err = C.Set(i, j, val)
if err != nil {
return Matrix[T]{}, err
}
val, err = C12.Get(i, j)
if err != nil {
return Matrix[T]{}, err
}
err1 := C.Set(i, j+mid, val)
if err1 != nil {
return Matrix[T]{}, err1
}
val, err = C21.Get(i, j)
if err != nil {
return Matrix[T]{}, err
}
err2 := C.Set(i+mid, j, val)
if err2 != nil {
return Matrix[T]{}, err2
}
val, err = C22.Get(i, j)
if err != nil {
return Matrix[T]{}, err
}
err3 := C.Set(i+mid, j+mid, val)
if err3 != nil {
return Matrix[T]{}, err3
}
}
}
return C, nil
}
}
================================================
FILE: math/matrix/strassenmatrixmultiply_test.go
================================================
package matrix_test
import (
"math/rand"
"testing"
"time"
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestStrassenMatrixMultiply(t *testing.T) {
// Create two sample matrices
dataA := [][]int{{1, 2}, {4, 5}}
dataB := [][]int{{9, 8}, {6, 5}}
matrixA, err := matrix.NewFromElements(dataA)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
matrixB, err := matrix.NewFromElements(dataB)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
// Perform matrix multiplication using Strassen's algorithm
resultMatrix, err := matrixA.StrassenMatrixMultiply(matrixB)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
// Expected result
expectedData, err := matrixA.Multiply(matrixB)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
// Check the dimensions of the result matrix
expectedRows := expectedData.Rows()
expectedColumns := expectedData.Columns()
rows := resultMatrix.Rows()
columns := resultMatrix.Columns()
if rows != expectedRows {
t.Errorf("Expected %d rows in result matrix, but got %d", expectedRows, rows)
}
if columns != expectedColumns {
t.Errorf("Expected %d columns in result matrix, but got %d", expectedColumns, columns)
}
// Check the values in the result matrix
for i := 0; i < expectedRows; i++ {
for j := 0; j < expectedColumns; j++ {
val, err := resultMatrix.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
expVal, err := expectedData.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
if val != expVal {
t.Errorf("Expected value %d at (%d, %d) in result matrix, but got %d", expVal, i, j, val)
}
}
}
}
func TestMatrixMultiplication(t *testing.T) {
rand.New(rand.NewSource(time.Now().UnixNano()))
// Generate random matrices for testing
size := 1 << (rand.Intn(8) + 1) // tests for matrix with n as power of 2
matrixA := MakeRandomMatrix[int](size, size)
matrixB := MakeRandomMatrix[int](size, size)
// Calculate the expected result using the standard multiplication
expected, err := matrixA.Multiply(matrixB)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
// Calculate the result using the Strassen algorithm
result, err := matrixA.StrassenMatrixMultiply(matrixB)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
// Check if the result matches the expected result
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
val, err := result.Get(i, j)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
exp, err := expected.Get(i, j)
if err != nil {
t.Error("copyMatrix.Set error: " + err.Error())
}
if val != exp {
t.Errorf("Mismatch at position (%d, %d). Expected %d, but got %d.", i, j, exp, val)
}
}
}
}
func MakeRandomMatrix[T constraints.Integer](rows, columns int) matrix.Matrix[T] {
rand.New(rand.NewSource(time.Now().UnixNano()))
matrixData := make([][]T, rows)
for i := 0; i < rows; i++ {
matrixData[i] = make([]T, columns)
for j := 0; j < columns; j++ {
matrixData[i][j] = T(rand.Intn(1000)) // Generate random integers between 0 and 1000
}
}
randomMatrix, _ := matrix.NewFromElements(matrixData)
return randomMatrix
}
// BenchmarkStrassenMatrixMultiply benchmarks the StrassenMatrixMultiply function.
func BenchmarkStrassenMatrixMultiply(b *testing.B) {
// Create sample matrices for benchmarking
rows := 64 // it is large enough for multiplication
columns := 64
m1 := matrix.New(rows, columns, 2) // Replace with appropriate values
m2 := matrix.New(rows, columns, 3) // Replace with appropriate values
for i := 0; i < b.N; i++ {
_, _ = m1.StrassenMatrixMultiply(m2)
}
}
================================================
FILE: math/matrix/string.go
================================================
package matrix
import "fmt"
// String implements the fmt.Stringer interface for Matrix.
func (m Matrix[T]) String() string {
var result string
for i := range m.elements {
for j := range m.elements[i] {
result += fmt.Sprintf("%v ", m.elements[i][j])
}
result += "\n"
}
return result
}
================================================
FILE: math/matrix/string_test.go
================================================
package matrix_test
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestMatrixString(t *testing.T) {
// Create a sample matrix for testing
m1, err := matrix.NewFromElements([][]int{{1, 2}, {3, 4}})
if err != nil {
t.Errorf("Error creating matrix: %v", err)
}
// Redirect stdout to capture Stringed output
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
os.Stdout = w
// Call the String method
fmt.Print(m1)
// Reset stdout
w.Close()
os.Stdout = old
// Read the captured output
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err != nil {
t.Errorf("Error copying output: %v", err)
}
capturedOutput := buf.String()
// Define the expected output
expectedOutput := "1 2 \n3 4 \n"
// Compare the captured output with the expected output
if capturedOutput != expectedOutput {
t.Errorf("Matrix.Print() produced incorrect output:\n%s\nExpected:\n%s", capturedOutput, expectedOutput)
}
}
func TestNullMatrixString(t *testing.T) {
m1 := matrix.New(0, 0, 0)
// Redirect stdout to capture Stringed output
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
os.Stdout = w
// Call the String method
fmt.Print(m1)
// Reset stdout
w.Close()
os.Stdout = old
// Read the captured output
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
if err != nil {
t.Errorf("Error copying output: %v", err)
}
capturedOutput := buf.String()
// Define the expected output
expectedOutput := ""
// Compare the captured output with the expected output
if capturedOutput != expectedOutput {
t.Errorf("Matrix.Print() produced incorrect output:\n%s\nExpected:\n%s", capturedOutput, expectedOutput)
}
}
func BenchmarkString(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
m := matrix.New(rows, columns, 0) // Replace with appropriate values
for i := 0; i < b.N; i++ {
_ = m.String()
}
}
================================================
FILE: math/matrix/submatrix.go
================================================
package matrix
import (
"context"
"errors"
"sync"
)
// SubMatrix extracts a submatrix from the current matrix.
func (m Matrix[T]) SubMatrix(rowStart, colStart, numRows, numCols int) (Matrix[T], error) {
if rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 {
return Matrix[T]{}, errors.New("negative dimensions are not allowed")
}
if rowStart+numRows > m.rows || colStart+numCols > m.columns {
return Matrix[T]{}, errors.New("submatrix dimensions exceed matrix bounds")
}
var zeroVal T
if numRows == 0 || numCols == 0 {
return New(numRows, numCols, zeroVal), nil // Return an empty matrix
}
subMatrix := New(numRows, numCols, zeroVal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Make sure it's called to release resources even if no errors
var wg sync.WaitGroup
errCh := make(chan error, 1)
for i := 0; i < numRows; i++ {
i := i // Capture the loop variable for the goroutine
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < numCols; j++ {
select {
case <-ctx.Done():
return // Context canceled; return without an error
default:
}
val, err := m.Get(rowStart+i, colStart+j)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
err = subMatrix.Set(i, j, val)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
}
}()
}
// Wait for all goroutines to finish
go func() {
wg.Wait()
close(errCh)
}()
// Check for any errors
if err := <-errCh; err != nil {
return Matrix[T]{}, err
}
return subMatrix, nil
}
================================================
FILE: math/matrix/submatrix_test.go
================================================
package matrix_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestMatrixSubMatrix(t *testing.T) {
// Create a sample matrix
data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
matrix, err := matrix.NewFromElements(data)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
// Extract a submatrix
subMatrix, err := matrix.SubMatrix(1, 1, 2, 2)
if err != nil {
t.Errorf("Error extracting submatrix: %v", err)
}
// Check the dimensions of the submatrix
expectedRows := 2
expectedColumns := 2
rows := subMatrix.Rows()
columns := subMatrix.Columns()
if rows != expectedRows {
t.Errorf("Expected %d rows in submatrix, but got %d", expectedRows, rows)
}
if columns != expectedColumns {
t.Errorf("Expected %d columns in submatrix, but got %d", expectedColumns, columns)
}
// Check the values in the submatrix
expectedData := [][]int{{5, 6}, {8, 9}}
for i := 0; i < expectedRows; i++ {
for j := 0; j < expectedColumns; j++ {
val, err := subMatrix.Get(i, j)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
if val != expectedData[i][j] {
t.Errorf("Expected value %d at (%d, %d) in submatrix, but got %d", expectedData[i][j], i, j, val)
}
}
}
}
func TestMatrixInvalidSubMatrix(t *testing.T) {
// Create a sample matrix
data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
matrix, err := matrix.NewFromElements(data)
if err != nil {
t.Fatalf("Failed to copy matrix: %v", err)
}
// Attempt to extract an invalid submatrix
_, err = matrix.SubMatrix(1, 1, 3, 3)
// Check if an error is returned
if err == nil {
t.Error("Expected an error for invalid submatrix dimensions, but got nil")
}
// Check the error message
expectedErrorMessage := "submatrix dimensions exceed matrix bounds"
if err.Error() != expectedErrorMessage {
t.Errorf("Expected error message '%s', but got '%s'", expectedErrorMessage, err.Error())
}
}
// BenchmarkSubMatrix benchmarks the SubMatrix function.
func BenchmarkSubMatrix(b *testing.B) {
// Create a sample matrix for benchmarking
rows := 100
columns := 100
matrix := matrix.New(rows, columns, 2) // Replace with appropriate values
rowStart := 10
colStart := 10
numRows := 20
numCols := 20
for i := 0; i < b.N; i++ {
_, _ = matrix.SubMatrix(rowStart, colStart, numRows, numCols)
}
}
================================================
FILE: math/matrix/subtract.go
================================================
package matrix
import (
"context"
"errors"
"sync"
)
// Subtract subtracts two matrices.
func (m1 Matrix[T]) Subtract(m2 Matrix[T]) (Matrix[T], error) {
// Check if the matrices have the same dimensions.
if !m1.MatchDimensions(m2) {
return Matrix[T]{}, errors.New("matrices are not compatible for subtraction")
}
// Create a new matrix to store the result.
var zeroVal T
result := New(m1.Rows(), m1.Columns(), zeroVal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Make sure it's called to release resources even if no errors
var wg sync.WaitGroup
errCh := make(chan error, 1)
for i := 0; i < m1.rows; i++ {
i := i // Capture the loop variable for the goroutine
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < m1.columns; j++ {
select {
case <-ctx.Done():
return // Context canceled; return without an error
default:
}
diff := m1.elements[i][j] - m2.elements[i][j]
err := result.Set(i, j, diff)
if err != nil {
cancel() // Cancel the context on error
select {
case errCh <- err:
default:
}
return
}
}
}()
}
// Wait for all goroutines to finish
go func() {
wg.Wait()
close(errCh)
}()
// Check for any errors
if err := <-errCh; err != nil {
return Matrix[T]{}, err
}
return result, nil
}
================================================
FILE: math/matrix/subtract_test.go
================================================
package matrix_test
import (
"fmt"
"testing"
"github.com/TheAlgorithms/Go/math/matrix"
)
func TestSubtract(t *testing.T) {
// Create two matrices with the same dimensions for Subtraction
m1 := matrix.New(2, 2, 1)
m2 := matrix.New(2, 2, 2)
// Test case 1: Valid matrix Subtraction
subMatrix, err := m1.Subtract(m2)
if err != nil {
t.Errorf("Add(m1, m2) returned an error: %v, expected no error", err)
}
expectedMatrix := matrix.New(2, 2, -1)
res := subMatrix.CheckEqual(expectedMatrix)
if !res {
t.Errorf("Add(m1, m2) returned incorrect result:\n%v\nExpected:\n%v", subMatrix, expectedMatrix)
}
// Create two matrices with different dimensions for Subtraction
m3 := matrix.New(2, 2, 1)
m4 := matrix.New(2, 3, 2)
// Test case 2: Matrices with different dimensions
_, err2 := m3.Subtract(m4)
expectedError2 := fmt.Errorf("matrices are not compatible for subtraction")
if err2 == nil || err2.Error() != expectedError2.Error() {
t.Errorf("m3.Subtract(m4) returned error: %v, expected error: %v", err2, expectedError2)
}
}
// BenchmarkSubtract benchmarks the Subtract function.
func BenchmarkSubtract(b *testing.B) {
// Create sample matrices for benchmarking
rows := 100
columns := 100
m1 := matrix.New(rows, columns, 2) // Replace with appropriate values
m2 := matrix.New(rows, columns, 3) // Replace with appropriate values
for i := 0; i < b.N; i++ {
_, _ = m1.Subtract(m2)
}
}
================================================
FILE: math/max/bitwisemax.go
================================================
// bitwiseMax.go
// description: Gives max of two integers
// details:
// implementation of finding the maximum of two numbers using only binary operations without using conditions
// author(s) [red_byte](https://github.com/i-redbyte)
// time complexity: O(1)
// space complexity: O(1)
// see bitwiseMax_test.go
package max
// Bitwise computes using bitwise operator the maximum of all the integer input and returns it
func Bitwise(a int, b int, base int) int {
z := a - b
i := (z >> base) & 1
return a - (i * z)
}
================================================
FILE: math/max/bitwisemax_test.go
================================================
// bitwiseMax_test.go
// description: Test for Bitwise
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitwiseMax.go
package max
import "testing"
func TestBitwiseMax(t *testing.T) {
base32 := 31
t.Run("Testing(32bit) a = 32 and m = 64: ", func(t *testing.T) {
max := Bitwise(32, 64, base32)
if max != 64 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
t.Run("Testing(32bit) a = 1024 and m = -9: ", func(t *testing.T) {
max := Bitwise(1024, -9, base32)
if max != 1024 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
t.Run("Testing(32bit) a = -6 and m = -6: ", func(t *testing.T) {
max := Bitwise(-6, -6, base32)
if max != -6 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
t.Run("Testing(32bit) a = -72 and m = -73: ", func(t *testing.T) {
max := Bitwise(-72, -73, base32)
if max != -72 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
base64 := 63
t.Run("Testing(64bit) a = 32 and m = 9223372036854775807: ", func(t *testing.T) {
max := Bitwise(32, 9223372036854775807, base64)
if max != 9223372036854775807 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
t.Run("Testing(64bit) a = 1024 and m = -9223372036854770001: ", func(t *testing.T) {
max := Bitwise(1024, -9223372036854770001, base64)
if max != 1024 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
t.Run("Testing(64bit) a = -6 and m = -6: ", func(t *testing.T) {
max := Bitwise(-6, -6, base64)
if max != -6 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
t.Run("Testing(64bit) a = -4223372036854775809 and m = -4223372036854775808: ", func(t *testing.T) {
max := Bitwise(-4223372036854775809, -4223372036854775808, base64)
if max != -4223372036854775808 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
base8 := 7
t.Run("Testing(8bit) a = 257 and m = 256: ", func(t *testing.T) {
max := Bitwise(8, 16, base8)
if max != 16 {
t.Fatalf("Error: Bitwise returned bad value")
}
})
}
================================================
FILE: math/max/max.go
================================================
package max
import "github.com/TheAlgorithms/Go/constraints"
// Int is a function which returns the maximum of all the integers provided as arguments.
func Int[T constraints.Integer](values ...T) T {
max := values[0]
for _, value := range values {
if value > max {
max = value
}
}
return max
}
================================================
FILE: math/max/max_test.go
================================================
package max
import "testing"
func TestMax(t *testing.T) {
testCases := []struct {
name string
left int
right int
max int
}{
{
name: "Left is max",
left: 10,
right: 9,
max: 10,
},
{
name: "right is max",
left: 1,
right: 10,
max: 10,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
returnedMax := Int(test.left, test.right)
if returnedMax != test.max {
t.Errorf("Failed test %s\n\tleft: %v, right: %v, max: %v but received: %v",
test.name, test.left, test.right, test.max, returnedMax)
}
})
}
}
func TestMaxOfThree(t *testing.T) {
testCases := []struct {
name string
left int
middle int
right int
max int
}{
{
name: "right is max",
left: 1,
middle: 5,
right: 10,
max: 10,
},
{
name: "left is max",
left: 10,
middle: 5,
right: 9,
max: 10,
},
{
name: "left is max",
left: 10,
middle: 8,
right: 6,
max: 10,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
actualMax := Int(test.left, test.middle, test.right)
if actualMax != test.max {
t.Errorf("Failed test %s\n\tleft: %v, middle: %v, right: %v, max: %v but received: %v",
test.name, test.left, test.middle, test.right, test.max, actualMax)
}
})
}
}
================================================
FILE: math/mean.go
================================================
package math
import (
"github.com/TheAlgorithms/Go/constraints"
)
func Mean[T constraints.Number](values []T) float64 {
if len(values) == 0 {
return 0
}
var summation float64 = 0
for _, singleValue := range values {
summation += float64(singleValue)
}
return summation / float64(len(values))
}
================================================
FILE: math/mean_test.go
================================================
package math_test
import (
"github.com/TheAlgorithms/Go/math"
"testing"
)
func TestMean(t *testing.T) {
testCases := []struct {
name string
testValues []float64
average float64
}{
{
name: "All 0s",
testValues: []float64{0, 0, 0, 0, 0},
average: 0,
},
{
name: "With integer values",
testValues: []float64{1, 2, 3, 4, 5},
average: 3.0,
},
{
name: "With negative values",
testValues: []float64{-1, 2, -3, 4, 5},
average: 1.4,
},
{
name: "With floating values",
testValues: []float64{1.1, 2.2, 3.3, 4.4, 5.5},
average: 3.3,
},
{
name: "With no values",
testValues: []float64{},
average: 0,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
returnedAverage := math.Mean(test.testValues)
if returnedAverage != test.average {
t.Errorf("\nFailed test: %s\ntestValues: %v\naverage: %v\nbut received: %v\n",
test.name, test.testValues, test.average, returnedAverage)
}
})
}
}
================================================
FILE: math/median.go
================================================
// author(s) [jo3zeph](https://github.com/jo3zeph)
// description: Find the median from a set of values
// time complexity: O(n log n)
// space complexity: O(1)
// see median_test.go
package math
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/sort"
)
func Median[T constraints.Number](values []T) float64 {
sort.Bubble(values)
l := len(values)
switch {
case l == 0:
return 0
case l%2 == 0:
return float64((values[l/2-1] + values[l/2]) / 2)
default:
return float64(values[l/2])
}
}
================================================
FILE: math/median_test.go
================================================
// author(s) [jo3zeph](https://github.com/jo3zeph)
// median_test.go
// see median.go
package math_test
import (
"testing"
"github.com/TheAlgorithms/Go/math"
)
func TestMedian(t *testing.T) {
testCases := []struct {
name string
testValues []float64
answer float64
}{
{
name: "Series of numbers in ascending order",
testValues: []float64{12, 14, 16, 18, 19},
answer: 16,
},
{
name: "Series of numbers in random order",
testValues: []float64{21, 10, 22, 33, 11, 88},
answer: 21.5,
},
{
name: "Series of decimals in random order",
testValues: []float64{11.2, 32.5, 2.5, 37.8, 21.8, 5.2},
answer: 16.5,
},
{
testValues: []float64{},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
returnedMedian := math.Median(test.testValues)
t.Log(test.testValues, " ", returnedMedian)
if returnedMedian != test.answer {
t.Errorf("Test failed. Median should have been %v but received %v",
test.answer, returnedMedian)
}
})
}
}
================================================
FILE: math/min/bitwisemin.go
================================================
// bitwisemin.go
// description: Gives min of two integers
// details:
// implementation of finding the minimum of two numbers using only binary operations without using conditions
// author(s) [red_byte](https://github.com/i-redbyte)
// see bitwisemin_test.go
package min
// Bitwise This function returns the minimum integer using bit operations
func Bitwise(base int, value int, values ...int) int {
min := value
for _, val := range values {
min = min&((min-val)>>base) | val&(^(min-val)>>base)
}
return min
}
================================================
FILE: math/min/min.go
================================================
package min
import "github.com/TheAlgorithms/Go/constraints"
// Int is a function which returns the minimum of all the integers provided as arguments.
func Int[T constraints.Integer](values ...T) T {
min := values[0]
for _, value := range values {
if value < min {
min = value
}
}
return min
}
================================================
FILE: math/min/min_test.go
================================================
package min
import (
"testing"
)
func getTestCases() []struct {
name string
base int
numbers []int
min int
} {
var tests = []struct {
name string
base int
numbers []int
min int
}{
{"Minimum of [128, 127], min = 117", 8, []int{128, 127}, 127},
{"Minimum of [5], min = 5", 32, []int{5}, 5},
{"Minimum of [-8, 32, 64, -1, 0], min = -8", 64, []int{-8, 32, 64, -1, 0}, -8},
{"Minimum of [1, 2, 3, 4, 5], min = 1", 32, []int{1, 2, 3, 4, 5}, 1},
{"Minimum of [1024, 512, 256, 333, 777], min = 256", 64, []int{1024, 512, 256, 333, 777}, 256},
{"Minimum of [-9223372036854770001, -9223372036854770000, 256, 333, 777], min = 256", 64, []int{-9223372036854770001, -9223372036854770000, 256, 333, 777}, -9223372036854770001},
}
return tests
}
func TestBitwiseMin(t *testing.T) {
tests := getTestCases()
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := Bitwise(test.base, 999, test.numbers...)
if result != test.min {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.min, result)
}
})
}
}
func TestMin(t *testing.T) {
for _, test := range getTestCases() {
t.Run(test.name, func(t *testing.T) {
actualMin := Int(test.numbers...)
if actualMin != test.min {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.min, actualMin)
}
})
}
}
func BenchmarkTestMinInt(b *testing.B) {
for i := 0; i < b.N; i++ {
Int(0, 10, 9, 7, 2, 1, 4, 3, 5, 6)
}
}
func BenchmarkTestBitwiseMin(b *testing.B) {
for i := 0; i < b.N; i++ {
Bitwise(32, 10, 9, 7, 2, 1, 4, 3, 5, 6)
}
}
================================================
FILE: math/mobius.go
================================================
// mobius.go
// description: Returns μ(n)
// details:
// For any positive integer n, define μ(n) as the sum of the primitive nth roots of unity.
// It has values in {−1, 0, 1} depending on the factorization of n into prime factors:
// μ(n) = +1 if n is a square-free positive integer with an even number of prime factors.
// μ(n) = −1 if n is a square-free positive integer with an odd number of prime factors.
// μ(n) = 0 if n has a squared prime factor.
// wikipedia: https://en.wikipedia.org/wiki/M%C3%B6bius_function
// time complexity: O(n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see mobius_test.go
package math
import (
"github.com/TheAlgorithms/Go/math/prime"
)
// Mu is the Mobius function
// This function returns μ(n) for given number
func Mu(n int) int {
if n <= 1 {
return 1
}
var primeFactorCount int
for i := 1; i <= n; i++ {
if n%i == 0 && prime.OptimizedTrialDivision(int64(i)) {
if n%(i*i) == 0 {
return 0
}
primeFactorCount += 1
}
}
if primeFactorCount%2 == 0 {
return 1
}
return -1
}
================================================
FILE: math/mobius_test.go
================================================
// mobius_test.go
// description: Returns μ(n)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see mobius.go
package math_test
import (
"testing"
algmath "github.com/TheAlgorithms/Go/math"
)
func TestMu(t *testing.T) {
var tests = []struct {
n int
expected int
}{
{-1, 1},
{0, 1},
{2, -1},
{3, -1},
{95, 1},
{97, -1},
{98, 0},
{99, 0},
{100, 0},
}
for _, test := range tests {
result := algmath.Mu(test.n)
t.Log(test.n, " ", result)
if result != test.expected {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.expected, result)
}
}
}
func BenchmarkMu(b *testing.B) {
for i := 0; i < b.N; i++ {
algmath.Mu(65536)
}
}
================================================
FILE: math/mode.go
================================================
// mode.go
// author(s): [CalvinNJK] (https://github.com/CalvinNJK)
// time complexity: O(n)
// space complexity: O(n)
// description: Finding Mode Value In an Array
// see mode.go
package math
import (
"errors"
"github.com/TheAlgorithms/Go/constraints"
)
// ErrEmptySlice is the error returned by functions in math package when
// an empty slice is provided to it as argument when the function expects
// a non-empty slice.
var ErrEmptySlice = errors.New("empty slice provided")
func Mode[T constraints.Number](numbers []T) (T, error) {
countMap := make(map[T]int)
n := len(numbers)
if n == 0 {
return 0, ErrEmptySlice
}
for _, number := range numbers {
countMap[number]++
}
var mode T
count := 0
for k, v := range countMap {
if v > count {
count = v
mode = k
}
}
return mode, nil
}
================================================
FILE: math/mode_test.go
================================================
// mode.go
// author(s): [CalvinNJK] (https://github.com/CalvinNJK)
// description: Test for Finding Mode Value In an Array
package math_test
import (
"errors"
"testing"
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math"
)
type testCase[T constraints.Number] struct {
name string
numbers []T
mode T
err error
}
func testModeFramework[T constraints.Number](t *testing.T, testCases []testCase[T]) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
returnMode, err := math.Mode(test.numbers)
if returnMode != test.mode {
t.Errorf("\n Failed test %s,\n Numbers: %v,\n Correct Mode: %v,\n Returned Mode: %v\n",
test.name, test.numbers, test.mode, returnMode)
}
if !errors.Is(err, test.err) {
t.Errorf("\n Failed test %s,\n Numbers: %v,\n Correct Error: %v,\n Returned Error: %v\n",
test.name, test.numbers, test.err, err)
}
})
}
}
func TestMode(t *testing.T) {
// test cases for integer values
intTestCases := []testCase[int]{
{
name: "An array of positive whole numbers",
numbers: []int{10, 52, 10, 92, 10, 75, 60, 10, 44, 29},
mode: 10,
err: nil,
},
{
name: "An array of negative whole numbers",
numbers: []int{-19, -12, -74, -19, -22, -56, -19, -19, -68, -93},
mode: -19,
err: nil,
},
{
name: "An array of positive & negative whole numbers",
numbers: []int{18, -28, 33, -28, 2, 39, 48, -49, -87, 78, -28},
mode: -28,
err: nil,
},
{
name: "If array has no value",
numbers: []int{},
mode: 0,
err: math.ErrEmptySlice,
},
}
testModeFramework(t, intTestCases)
// test cases for float64 values
floatTestCases := []testCase[float64]{
{
name: "An array of positive real numbers",
numbers: []float64{1.5, 2.88, 84.4, 77.2, 29.8, 46.2, 33.7, 88.4, 88.4},
mode: 88.4,
err: nil,
},
{
name: "An array of negative real numbers",
numbers: []float64{-98.1, -26.8, -54.45, -26.8, -1.5, -26.8, -33, -19.5, -26.8},
mode: -26.8,
err: nil,
},
{
name: "An array of positive and negative real numbers",
numbers: []float64{-17, 28.9, -5.2, -19.5, 77.3, -5.2, 39.3, 28.5, -59.77, -5.2},
mode: -5.2,
err: nil,
},
{
name: "If array has no value",
numbers: []float64{},
mode: 0,
err: math.ErrEmptySlice,
},
}
testModeFramework(t, floatTestCases)
}
================================================
FILE: math/modular/exponentiation.go
================================================
// exponentiation.go
// description: Implementation of Modular Exponentiation Algorithm
// details:
// A simple implementation of Modular Exponentiation - [Modular Exponenetation wiki](https://en.wikipedia.org/wiki/Modular_exponentiation)
// time complexity: O(log(n)) where n is the exponent
// space complexity: O(1)
// author(s) [Taj](https://github.com/tjgurwara99)
// see exponentiation_test.go
package modular
import (
"errors"
"math"
)
// ErrorIntOverflow For asserting that the values do not overflow in Int64
var ErrorIntOverflow = errors.New("integer overflow")
// ErrorNegativeExponent for asserting that the exponent we receive is positive
var ErrorNegativeExponent = errors.New("negative Exponent provided")
// Exponentiation returns base^exponent % mod
func Exponentiation(base, exponent, mod int64) (int64, error) {
if mod == 1 {
return 0, nil
}
if exponent < 0 {
return -1, ErrorNegativeExponent
}
_, err := Multiply64BitInt(mod-1, mod-1)
if err != nil {
return -1, err
}
var result int64 = 1
base = base % mod
for exponent > 0 {
if exponent%2 == 1 {
result = (result * base) % mod
}
exponent = exponent >> 1
base = (base * base) % mod
}
return result, nil
}
// Multiply64BitInt Checking if the integer multiplication overflows
func Multiply64BitInt(left, right int64) (int64, error) {
if math.Abs(float64(left)) > float64(math.MaxInt64)/math.Abs(float64(right)) {
return 0, ErrorIntOverflow
}
return left * right, nil
}
================================================
FILE: math/modular/exponentiation_test.go
================================================
// exponentiation_test.go
// description: Test for ModularExponentiation
// author(s) [Taj](https://github.com/tjgurwara99)
// see exponentiation.go
package modular
import "testing"
type cases struct {
name string
description string
base int64
exponent int64
mod int64
expected int64
expectedError error
}
var testCases = []cases{
{
name: "Test 1",
description: "Test 1: 3^6 % 3 == 0",
base: 3,
exponent: 6,
mod: 3,
expected: 0,
expectedError: nil,
},
{
name: "Test 2",
description: "Test 2: 33^60 % 25 == 1",
base: 33,
exponent: 60,
mod: 25,
expected: 1,
expectedError: nil,
},
{
name: "Test 3",
description: "Test 3: 17^60 % 23 == 2",
base: 17,
exponent: 60,
mod: 23,
expected: 2,
expectedError: nil,
},
{
name: "Test 4",
description: "Test 4: 17^60 % 1 == 0", // handling result when we get mod = 1
base: 17,
exponent: 60,
mod: 1,
expected: 0,
expectedError: nil,
},
{
name: "Error test 1",
description: "Testing whether we receive the expected errors gracefully",
base: 50,
exponent: -1,
mod: 2,
expected: -1,
expectedError: ErrorNegativeExponent,
},
}
func TestExponentiation(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
result, err := Exponentiation(test.base, test.exponent, test.mod)
if err != test.expectedError {
t.Logf("Test Failed for %s", test.name)
t.Logf("Unexpected error occurred")
t.Errorf("Expected error: %v, Received error: %v", test.expectedError, err)
}
if result != test.expected {
t.Logf("Test Failed for %s", test.description)
t.Fatalf("Expected: %d, Received: %d", test.expected, result)
}
})
}
}
func BenchmarkExponentiation(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Exponentiation(17, 60, 23)
}
}
================================================
FILE: math/modular/inverse.go
================================================
// inverse.go
// description: Implementation of Modular Inverse Algorithm
// details:
// A simple implementation of Modular Inverse - [Modular Inverse wiki](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse)
// time complexity: O(log(min(a, b))) where a and b are the two numbers
// space complexity: O(1)
// author(s) [Taj](https://github.com/tjgurwara99)
// see inverse_test.go
package modular
import (
"errors"
"github.com/TheAlgorithms/Go/math/gcd"
)
var ErrorInverse = errors.New("no Modular Inverse exists")
// Inverse Modular function
func Inverse(a, m int64) (int64, error) {
gcd, x, _ := gcd.Extended(a, m)
if gcd != 1 || m == 0 {
return 0, ErrorInverse
}
return ((m + (x % m)) % m), nil // this is necessary because of Go's use of architecture specific instruction for the % operator.
}
================================================
FILE: math/modular/inverse_test.go
================================================
// inverse_test.go
// description: Test for Modular Inverse
// author(s) [Taj](https://github.com/tjgurwara99)
// see inverse.go
package modular
import "testing"
import "fmt"
func TestInverse(t *testing.T) {
testCases := []struct {
a int64
m int64
expectedValue int64
expectedError error
}{
{3, 11, 4, nil},
{10, 17, 12, nil},
{2, 6, 0, ErrorInverse},
{1, 0, 0, ErrorInverse},
}
for _, tc := range testCases {
testName := fmt.Sprintf("Testing a = %d and m = %d: ", tc.a, tc.m)
t.Run(testName, func(t *testing.T) {
inv, err := Inverse(tc.a, tc.m)
if err != tc.expectedError {
if tc.expectedError == nil {
t.Fatalf("Error was raised when it shouldn't: %v", err)
} else {
t.Fatalf("Error was not raised when it should")
}
}
if inv != tc.expectedValue {
t.Fatalf("expected: %d, got: %d", tc.expectedValue, inv)
}
})
}
}
================================================
FILE: math/moserdebruijnsequence/sequence.go
================================================
// The Moser-de Bruijn sequence is the sequence obtained by
// adding up the distinct powers of the number 4 (For example 1, 4, 16, 64, etc).
// time complexity: O(n)
// space complexity: O(n)
// You can get more details on https://en.wikipedia.org/wiki/Moser%E2%80%93de_Bruijn_sequence.
package moserdebruijnsequence
func MoserDeBruijnSequence(number int) []int {
sequence := []int{}
for i := 0; i < number; i++ {
res := generateNthTerm(i)
sequence = append(sequence, res)
}
return sequence
}
func generateNthTerm(num int) int {
if num == 0 || num == 1 {
return num
}
//number is even
if num%2 == 0 {
return 4 * generateNthTerm(num/2)
}
//number is odd
return 4*generateNthTerm(num/2) + 1
}
================================================
FILE: math/moserdebruijnsequence/sequence_test.go
================================================
package moserdebruijnsequence
import (
"reflect"
"testing"
)
var testCases = []struct {
name string
inputNum int
expected []int
}{
{"first 15 terms", 15, []int{0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84}},
}
func TestMoserDeBruijnSequence(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
if output := MoserDeBruijnSequence(test.inputNum); !reflect.DeepEqual(output, test.expected) {
t.Errorf("For input: %d, expected: %v, but got: %v", test.inputNum, test.expected, output)
}
})
}
}
================================================
FILE: math/pascal/pascaltriangle.go
================================================
// pascaltriangle.go
// description: Pascal's triangle
// details:
// Pascal's triangle is a triangular array of the binomial coefficients that arises in probability theory, combinatorics, and algebra. - [Pascal's triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle)
// example:
//1
//1 1
//1 2 1
//1 3 3 1
//1 4 6 4 1
//1 5 10 10 5 1
//1 6 15 20 15 6 1
//1 7 21 35 35 21 7 1
//1 8 28 56 70 56 28 8 1
//1 9 36 84 126 126 84 36 9 1
//1 10 45 120 210 252 210 120 45 10 1
//...
// author(s) [red_byte](https://github.com/i-redbyte)
// time complexity: O(n^2)
// space complexity: O(n^2)
// see pascaltriangle_test.go
package pascal
// GenerateTriangle This function generates a Pascal's triangle of n lines
func GenerateTriangle(n int) [][]int {
var triangle = make([][]int, n)
for i := 0; i < n; i++ {
triangle[i] = make([]int, i+1)
triangle[i][0], triangle[i][i] = 1, 1
for j := 1; j < i; j++ {
triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1]
}
}
return triangle
}
================================================
FILE: math/pascal/pascaltriangle_test.go
================================================
// pascaltriangle_test.go
// description: Test for Pascal's triangle
// author(s) [red_byte](https://github.com/i-redbyte)
// see pascaltriangle.go
package pascal
import (
"reflect"
"testing"
)
func TestGenerateTriangle(t *testing.T) {
tests := []struct {
name string
n int
want [][]int
}{
{name: "Pascal's three-line triangle", n: 3, want: [][]int{{1}, {1, 1}, {1, 2, 1}}},
{name: "Pascal's 0-line triangle", n: 0, want: [][]int{}},
{name: "Pascal's one-line triangle", n: 1, want: [][]int{{1}}},
{name: "Pascal's 7-line triangle", n: 7, want: [][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1}, {1, 6, 15, 20, 15, 6, 1}}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := GenerateTriangle(test.n); !reflect.DeepEqual(got, test.want) {
t.Errorf("GenerateTriangle() = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkGenerateTriangle(b *testing.B) {
for i := 0; i < b.N; i++ {
GenerateTriangle(10)
}
}
================================================
FILE: math/perfectnumber.go
================================================
// perfectnumber.go
// description: provides the function IsPerfectNumber and related utilities
// details:
// provides the functions
// - IsPerfectNumber which checks if the input is a perfect number,
// - SumOfProperDivisors which returns the sum of proper divisors of the input.
// A number is called perfect, if it is a sum of its proper divisors,
// cf. https://en.wikipedia.org/wiki/Perfect_number,
// https://mathworld.wolfram.com/PerfectNumber.html
// time complexity: O(sqrt(n))
// space complexity: O(1)
// https://oeis.org/A000396
// author(s) [Piotr Idzik](https://github.com/vil02)
// see perfectnumber_test.go
package math
// Returns the sum of proper divisors of inNumber.
func SumOfProperDivisors(inNumber uint) uint {
var res = uint(0)
if inNumber > 1 {
res = uint(1)
}
for curDivisor := uint(2); curDivisor*curDivisor <= inNumber; curDivisor++ {
if inNumber%curDivisor == 0 {
res += curDivisor
if curDivisor*curDivisor != inNumber {
res += inNumber / curDivisor
}
}
}
return res
}
// Checks if inNumber is a perfect number
func IsPerfectNumber(inNumber uint) bool {
return inNumber > 0 && SumOfProperDivisors(inNumber) == inNumber
}
================================================
FILE: math/perfectnumber_test.go
================================================
package math_test
import (
"testing"
"github.com/TheAlgorithms/Go/math"
)
type testCaseSumOfProperDivisors struct {
input uint
expected uint
}
// getSumOfProperDivisorsTestCases returns an array of test data
// for the tests of the function SumOfProperDivisors.
// Data was verified using [A001065].
// [A001065]: https://oeis.org/A001065/b001065.txt
func getSumOfProperDivisorsTestCases() []testCaseSumOfProperDivisors {
return []testCaseSumOfProperDivisors{
{0, 0},
{1, 0},
{2, 1},
{3, 1},
{4, 3},
{5, 1},
{6, 6},
{7, 1},
{8, 7},
{9, 4},
{10, 8},
{11, 1},
{12, 16},
{13, 1},
{14, 10},
{15, 9},
{16, 15},
{17, 1},
{18, 21},
{19, 1},
{20, 22},
{21, 11},
{22, 14},
{23, 1},
{24, 36},
{25, 6},
{26, 16},
{27, 13},
{28, 28},
{29, 1},
{30, 42},
{31, 1},
{32, 31},
{33, 15},
{34, 20},
{35, 13},
{36, 55},
{37, 1},
{38, 22},
{39, 17},
{40, 50},
{41, 1},
{42, 54},
{43, 1},
{44, 40},
{45, 33},
{46, 26},
{47, 1},
{48, 76},
{49, 8},
{50, 43},
{51, 21},
{52, 46},
{53, 1},
{54, 66},
{55, 17},
{56, 64},
{57, 23},
{58, 32},
{59, 1},
{60, 108},
{800, 1153},
{8204, 8260},
{9646, 8498},
{9756, 14996},
{9829, 1},
{9954, 15006},
{10000, 14211},
}
}
func TestSumOfProperDivisors(t *testing.T) {
for _, tc := range getSumOfProperDivisorsTestCases() {
actual := math.SumOfProperDivisors(tc.input)
if actual != tc.expected {
t.Errorf(
"Expected SumOfProperDivisors(%d) to be: %d, but got: %d",
tc.input, tc.expected, actual)
}
}
}
func BenchmarkSumOfProperDivisors(b *testing.B) {
for i := 0; i < b.N; i++ {
math.SumOfProperDivisors(800)
}
}
type isPerfectNumberTestCase struct {
number uint
isPerfect bool
}
// getIsPerfectNumberTestCases returns an array of test data
// for the tests of the function IsPerfectNumber
// Data was verified using [A000396].
// [A000396]: https://oeis.org/A000396
func getIsPerfectNumberTestCases() []isPerfectNumberTestCase {
return []isPerfectNumberTestCase{
{6, true},
{28, true},
{496, true},
{8128, true},
{33550336, true},
{0, false},
{1, false},
{2, false},
{3, false},
{4, false},
{5, false},
{7, false},
{100, false},
{219, false},
{997, false},
{33550335, false},
{33550337, false},
}
}
func TestIsPerfectNumber(t *testing.T) {
for _, tc := range getIsPerfectNumberTestCases() {
if math.IsPerfectNumber(tc.number) != tc.isPerfect {
t.Errorf("Unexpected output for %d", tc.number)
}
}
}
func BenchmarkIsPerfectNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
math.IsPerfectNumber(8128)
}
}
================================================
FILE: math/permutation/heaps.go
================================================
// heaps.go
// description: Implementation of Heap's Algorithm for generating all permutations of n objects
// time complexity: O(n!)
// space complexity: O(n)
package permutation
import (
"strings"
)
// Heap's Algorithm for generating all permutations of n objects
func Heaps(out chan []string, n int) {
elementSetCh := make(chan []string)
go GenerateElementSet(elementSetCh, n)
elementSet := <-elementSetCh
var recursiveGenerate func([]string, int, []string)
var permutations []string
recursiveGenerate = func(previousIteration []string, n int, elements []string) {
if n == 1 {
permutations = append(permutations, strings.Join(elements, ""))
} else {
for i := 0; i < n; i++ {
recursiveGenerate(previousIteration, n-1, elements)
if n%2 == 1 {
tmp := elements[i]
elements[i] = elements[n-1]
elements[n-1] = tmp
} else {
tmp := elements[0]
elements[0] = elements[n-1]
elements[n-1] = tmp
}
}
}
}
recursiveGenerate(permutations, n, elementSet)
out <- permutations
}
func GenerateElementSet(out chan []string, n int) {
elementSet := make([]string, n)
for i := range elementSet {
elementSet[i] = string(rune(i + 49)) // Adjust this if you want to change your charset
}
out <- elementSet
}
================================================
FILE: math/permutation/heaps_test.go
================================================
package permutation
import (
"reflect"
"sort"
"testing"
)
func TestHeaps(t *testing.T) {
t.Run("Should generate permutations for various size sets", func(t *testing.T) {
expectedValues := [][]string{
{"1"},
{"12", "21"},
{"123", "213", "321", "231", "312", "132"},
{"1234", "1243", "1324", "1342", "1423", "1432", "2134", "2143", "2314", "2341", "2413", "2431", "3124", "3142", "3214", "3241", "3412", "3421", "4123", "4132", "4213", "4231", "4312", "4321"},
{"12345", "12354", "12435", "12453", "12534", "12543", "13245", "13254", "13425", "13452", "13524", "13542", "14235", "14253", "14325", "14352", "14523", "14532", "15234", "15243", "15324", "15342", "15423", "15432", "21345", "21354", "21435", "21453", "21534", "21543", "23145", "23154", "23415", "23451", "23514", "23541", "24135", "24153", "24315", "24351", "24513", "24531", "25134", "25143", "25314", "25341", "25413", "25431", "31245", "31254", "31425", "31452", "31524", "31542", "32145", "32154", "32415", "32451", "32514", "32541", "34125", "34152", "34215", "34251", "34512", "34521", "35124", "35142", "35214", "35241", "35412", "35421", "41235", "41253", "41325", "41352", "41523", "41532", "42135", "42153", "42315", "42351", "42513", "42531", "43125", "43152", "43215", "43251", "43512", "43521", "45123", "45132", "45213", "45231", "45312", "45321", "51234", "51243", "51324", "51342", "51423", "51432", "52134", "52143", "52314", "52341", "52413", "52431", "53124", "53142", "53214", "53241", "53412", "53421", "54123", "54132", "54213", "54231", "54312", "54321"},
}
permutationsCh := make(chan []string)
var value []string
for i := 1; i <= 5; i++ {
go Heaps(permutationsCh, i)
value = <-permutationsCh
sort.Strings(value)
sort.Strings(expectedValues[i-1])
if !reflect.DeepEqual(value, expectedValues[i-1]) {
t.Errorf("Permutation set is incorrect for element size of %v. Expected (%v) and received (%v)", i, expectedValues[i-1], value)
}
}
})
}
================================================
FILE: math/permutation/next_permutation.go
================================================
// A practice to find lexicographically next greater permutation of the given array of integers.
// If there does not exist any greater permutation, then print the lexicographically smallest permutation of the given array.
// The implementation below, finds the next permutation in linear time and constant memory and returns in place
// time complexity: O(n)
// space complexity: O(1)
// Useful reference: https://www.geeksforgeeks.org/next-permutation/
package permutation
func NextPermutation(nums []int) {
pivot := 0
for pivot = len(nums) - 2; pivot >= 0; pivot-- {
if nums[pivot] < nums[pivot+1] {
break
}
}
if pivot < 0 {
// current permutation is the last and must be reversed totally
for l, r := 0, len(nums)-1; l < r; l, r = l+1, r-1 {
nums[l], nums[r] = nums[r], nums[l]
}
} else {
succ := 0
for succ = len(nums) - 1; succ > pivot; succ = succ - 1 {
if nums[succ] > nums[pivot] {
break
}
}
// Swap the pivot and successor
nums[pivot], nums[succ] = nums[succ], nums[pivot]
// Reverse the suffix part to minimize it
for l, r := pivot+1, len(nums)-1; l < r; l, r = l+1, r-1 {
nums[l], nums[r] = nums[r], nums[l]
}
}
}
================================================
FILE: math/permutation/next_permutation_test.go
================================================
package permutation
import (
"reflect"
"testing"
)
func TestNextPermutation(t *testing.T) {
var nextPermutationTestData = []struct {
description string
numbers []int
next []int
}{
{
description: "Basic case",
numbers: []int{1, 2, 3},
next: []int{1, 3, 2},
},
{
description: "Should reverse the whole slice",
numbers: []int{3, 2, 1},
next: []int{1, 2, 3},
},
{
description: "A more complex test",
numbers: []int{2, 4, 1, 7, 5, 0},
next: []int{2, 4, 5, 0, 1, 7},
},
}
for _, test := range nextPermutationTestData {
t.Run(test.description, func(t *testing.T) {
NextPermutation(test.numbers)
if !reflect.DeepEqual(test.numbers, test.next) {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expected result:%v\nFound: %v", test.next, test.numbers)
}
})
}
}
================================================
FILE: math/pi/montecarlopi.go
================================================
// montecarlopi.go
// description: Calculating pi by the Monte Carlo method
// details:
// implementations of Monte Carlo Algorithm for the calculating of Pi - [Monte Carlo method](https://en.wikipedia.org/wiki/Monte_Carlo_method)
// time complexity: O(n)
// space complexity: O(1)
// author(s): [red_byte](https://github.com/i-redbyte), [Paul Leydier] (https://github.com/paul-leydier)
// see montecarlopi_test.go
package pi
import (
"fmt" // Used for error formatting
"math/rand" // Used for random number generation in Monte Carlo method
"runtime" // Used to get information on available CPUs
"time" // Used for seeding the random number generation
)
func MonteCarloPi(randomPoints int) float64 {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
inside := 0
for i := 0; i < randomPoints; i++ {
x := rnd.Float64()
y := rnd.Float64()
if x*x+y*y <= 1 {
inside += 1
}
}
pi := float64(inside) / float64(randomPoints) * 4
return pi
}
// MonteCarloPiConcurrent approximates the value of pi using the Monte Carlo method.
// Unlike the MonteCarloPi function (first version), this implementation uses
// goroutines and channels to parallelize the computation.
// More details on the Monte Carlo method available at https://en.wikipedia.org/wiki/Monte_Carlo_method.
// More details on goroutines parallelization available at https://go.dev/doc/effective_go#parallel.
func MonteCarloPiConcurrent(n int) (float64, error) {
numCPU := runtime.GOMAXPROCS(0)
c := make(chan int, numCPU)
pointsToDraw, err := splitInt(n, numCPU) // split the task in sub-tasks of approximately equal sizes
if err != nil {
return 0, err
}
// launch numCPU parallel tasks
for _, p := range pointsToDraw {
go drawPoints(p, c)
}
// collect the tasks results
inside := 0
for i := 0; i < numCPU; i++ {
inside += <-c
}
return float64(inside) / float64(n) * 4, nil
}
// drawPoints draws n random two-dimensional points in the interval [0, 1), [0, 1) and sends through c
// the number of points which where within the circle of center 0 and radius 1 (unit circle)
func drawPoints(n int, c chan<- int) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
inside := 0
for i := 0; i < n; i++ {
x, y := rnd.Float64(), rnd.Float64()
if x*x+y*y <= 1 {
inside++
}
}
c <- inside
}
// splitInt takes an integer x and splits it within an integer slice of length n in the most uniform
// way possible.
// For example, splitInt(10, 3) will return []int{4, 3, 3}, nil
func splitInt(x int, n int) ([]int, error) {
if x < n {
return nil, fmt.Errorf("x must be < n - given values are x=%d, n=%d", x, n)
}
split := make([]int, n)
if x%n == 0 {
for i := 0; i < n; i++ {
split[i] = x / n
}
} else {
limit := x % n
for i := 0; i < limit; i++ {
split[i] = x/n + 1
}
for i := limit; i < n; i++ {
split[i] = x / n
}
}
return split, nil
}
================================================
FILE: math/pi/montecarlopi_test.go
================================================
// montecarlopi_test.go
// description: Test for calculating pi by the Monte Carlo method
// author(s) [red_byte](https://github.com/i-redbyte)
// see montecarlopi.go
package pi
import (
"fmt"
"testing"
)
func TestMonteCarloPi(t *testing.T) {
t.Run("Monte Carlo Pi", func(t *testing.T) {
result := fmt.Sprintf("%.2f", MonteCarloPi(100000000))
t.Log(result)
if result != "3.14" {
t.Errorf("Wrong result! Expected:%f, returned:%s ", 3.1415, result)
}
})
}
func BenchmarkMonteCarloPi(b *testing.B) {
for i := 0; i < b.N; i++ {
MonteCarloPi(100000000)
}
}
func TestSplitInt(t *testing.T) {
testCases := []struct {
name string
x int
n int
expectedResult []int
expectedError bool
}{
{"multiple", 10, 5, []int{2, 2, 2, 2, 2}, false},
{"n=1", 10, 1, []int{10}, false},
{"x=10, n=3", 10, 3, []int{4, 3, 3}, false},
{"x=10, n=7", 10, 7, []int{2, 2, 2, 1, 1, 1, 1}, false},
{"n>x", 10, 11, nil, true},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
res, err := splitInt(testCase.x, testCase.n)
for i := 0; i < len(testCase.expectedResult); i++ {
if res[i] != testCase.expectedResult[i] {
t.Fatalf("unexpected result at index %d: %d - expected %d", i, res[i], testCase.expectedResult[i])
}
}
if testCase.expectedError {
if err == nil {
t.Fatal("expected an error, got nil")
}
} else {
if err != nil {
t.Fatalf("unexpected error - %s", err)
}
}
})
}
}
func TestMonteCarloPi2(t *testing.T) {
res, err := MonteCarloPiConcurrent(100000000)
if err != nil {
t.Errorf("unexpected error %s", err)
}
result := fmt.Sprintf("%.2f", res)
t.Log(result)
if result != "3.14" {
t.Errorf("Wrong result! Expected:%f, returned:%s ", 3.1415, result)
}
}
func BenchmarkMonteCarloPi2(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := MonteCarloPiConcurrent(100000000)
if err != nil {
b.Fatalf("unexpected error - %s", err)
}
}
}
================================================
FILE: math/pi/spigotpi.go
================================================
// spigotpi.go
// description: A Spigot Algorithm for the Digits of Pi
// details:
// implementation of Spigot Algorithm for the Digits of Pi - [Spigot algorithm](https://en.wikipedia.org/wiki/Spigot_algorithm)
// time complexity: O(n)
// space complexity: O(n)
// author(s) [red_byte](https://github.com/i-redbyte)
// see spigotpi_test.go
package pi
import "strconv"
func Spigot(n int) string {
pi := ""
boxes := n * 10 / 3
remainders := make([]int, boxes)
for i := 0; i < boxes; i++ {
remainders[i] = 2
}
digitsHeld := 0
for i := 0; i < n; i++ {
carriedOver := 0
sum := 0
for j := boxes - 1; j >= 0; j-- {
remainders[j] *= 10
sum = remainders[j] + carriedOver
quotient := sum / (j*2 + 1)
remainders[j] = sum % (j*2 + 1)
carriedOver = quotient * j
}
remainders[0] = sum % 10
q := sum / 10
switch q {
case 9:
digitsHeld++
case 10:
q = 0
for k := 1; k <= digitsHeld; k++ {
replaced, _ := strconv.Atoi(pi[i-k : i-k+1])
if replaced == 9 {
replaced = 0
} else {
replaced++
}
pi = delChar(pi, i-k)
pi = pi[:i-k] + strconv.Itoa(replaced) + pi[i-k:]
}
digitsHeld = 1
default:
digitsHeld = 1
}
pi += strconv.Itoa(q)
}
return pi
}
func delChar(s string, index int) string {
tmp := []rune(s)
return string(append(tmp[0:index], tmp[index+1:]...))
}
================================================
FILE: math/pi/spigotpi_test.go
================================================
// spigotpi_test.go
// description: Test for Spigot Algorithm for the Digits of Pi
// author(s) [red_byte](https://github.com/i-redbyte)
// see spigotpi.go
package pi
import (
"strconv"
"testing"
)
func TestSpigot(t *testing.T) {
var tests = []struct {
result string
n int
}{
{"314", 3},
{"31415", 5},
{"314159", 6},
{"31415926535", 11},
{"314159265358", 12},
{"314159265358979323846", 21},
{"3141592653589793238462643", 25},
{"314159265358979323846264338327950", 33},
{"31415926535897932384626433832795028841971693993751" +
"05820974944592307816406286208998628034825342117067" +
"98214808651328230664709384460955058223172535940812" +
"84811174502841027019385211055596446229489549303819" +
"64428810975665933446128475648233786783165271201909" +
"14564856692346034861045432664821339360726024914127" +
"37245870066063155881748815209209628292540917153643" +
"678925903600", 362},
}
for _, tv := range tests {
t.Run(strconv.Itoa(tv.n)+":"+tv.result, func(t *testing.T) {
result := Spigot(tv.n)
if result != tv.result {
t.Errorf("Bad result %d:%s", tv.n, tv.result)
}
})
}
}
func BenchmarkPiSpigotN10(b *testing.B) {
for i := 0; i < b.N; i++ {
Spigot(10)
}
}
func BenchmarkPiSpigotN100(b *testing.B) {
for i := 0; i < b.N; i++ {
Spigot(100)
}
}
func BenchmarkPiSpigotN1000(b *testing.B) {
for i := 0; i < b.N; i++ {
Spigot(1000)
}
}
================================================
FILE: math/pollard.go
================================================
// pollard.go
// description: Pollard's rho algorithm
// details:
// implementation of Pollard's rho algorithm for integer factorization-[Pollard's rho algorithm](https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm)
// time complexity: O(n^(1/4))
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see pollard_test.go
package math
import (
"errors"
"math/big"
)
// DefaultPolynomial is the commonly used polynomial g(x) = (x^2 + 1) mod n
func DefaultPolynomial(n *big.Int) func(*big.Int) *big.Int {
bigOne := big.NewInt(1)
bigTwo := big.NewInt(2)
return func(x *big.Int) *big.Int {
xSquared := new(big.Int).Exp(x, bigTwo, n) // see: https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm#Core_ideas
xSquared.Add(xSquared, bigOne)
xSquared.Mod(xSquared, n)
return xSquared
}
}
// PollardsRhoFactorization is an implementation of Pollard's rho factorization algorithm
// using the default parameters x = y = 2
func PollardsRhoFactorization(n *big.Int, f func(n *big.Int) func(x *big.Int) *big.Int) (*big.Int, error) {
x, y, d := big.NewInt(2), big.NewInt(2), big.NewInt(1)
bigOne := big.NewInt(1)
g := f(n)
for d.Cmp(bigOne) == 0 {
x = g(x)
y = g(g(y))
sub := new(big.Int).Sub(x, y)
d.GCD(nil, nil, sub.Abs(sub), n)
}
if d.Cmp(n) == 0 {
return nil, errors.New("factorization failed")
}
return d, nil
}
================================================
FILE: math/pollard_test.go
================================================
// pollard_test.go
// description: Test for Pollard's rho algorithm
// author(s) [red_byte](https://github.com/i-redbyte)
// see pollard.go
package math
import (
"math/big"
"reflect"
"testing"
)
func TestDefaultPolynomial(t *testing.T) {
tests := []struct {
name string
x *big.Int
n *big.Int
want *big.Int
}{
{"Polynomial for n = 1772; x = 535", big.NewInt(535), big.NewInt(1772), big.NewInt(934)},
{"Polynomial for n = 666; x = 53135", big.NewInt(53135), big.NewInt(666), big.NewInt(380)},
{"Polynomial for n = 666; x = 13", big.NewInt(13), big.NewInt(666), big.NewInt(170)},
{"Polynomial for n = 1917; x = 2510", big.NewInt(2510), big.NewInt(1917), big.NewInt(839)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := DefaultPolynomial(test.n)(test.x); !reflect.DeepEqual(got, test.want) {
t.Errorf("DefaultPolynomial() = %v, want %v", got, test.want)
}
})
}
}
func TestRho(t *testing.T) {
tests := []struct {
name string
n *big.Int
g func(n *big.Int) func(*big.Int) *big.Int
want *big.Int
wantErr bool
}{
{"Factor of n: 11235 ", big.NewInt(11235), DefaultPolynomial, big.NewInt(21), false},
{"Factor of n: 111155 ", big.NewInt(111155), DefaultPolynomial, big.NewInt(11), false},
{"Factor of n: 8080 ", big.NewInt(8080), DefaultPolynomial, big.NewInt(16), false},
{"Factor of n: 8536 ", big.NewInt(8536), DefaultPolynomial, big.NewInt(88), false},
{"Factor of n: 666 ", big.NewInt(666), DefaultPolynomial, big.NewInt(3), false},
{"Factor of n: 2 ", big.NewInt(2), DefaultPolynomial, big.NewInt(2), true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := PollardsRhoFactorization(test.n, test.g)
if err != nil && !test.wantErr {
t.Errorf("PollardsRhoFactorization() error = %v, wantErr %v", err, test.wantErr)
return
} else if err != nil && test.wantErr {
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("PollardsRhoFactorization() got = %v, want %v", got, test.want)
}
})
}
}
func BenchmarkDefaultPolynomial(b *testing.B) {
for i := 0; i < b.N; i++ {
DefaultPolynomial(big.NewInt(535))(big.NewInt(11235))
}
}
func BenchmarkRho(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := PollardsRhoFactorization(big.NewInt(535), DefaultPolynomial)
if err != nil {
return
}
}
}
================================================
FILE: math/power/fastexponent.go
================================================
package power
// IterativePower is iterative O(logn) function for pow(x, y)
func IterativePower(n uint, power uint) uint {
var res uint = 1
for power > 0 {
if (power & 1) != 0 {
res = res * n
}
power = power >> 1
n *= n
}
return res
}
// RecursivePower is recursive O(logn) function for pow(x, y)
func RecursivePower(n uint, power uint) uint {
if power == 0 {
return 1
}
var temp = RecursivePower(n, power/2)
if power%2 == 0 {
return temp * temp
}
return n * temp * temp
}
// RecursivePower1 is recursive O(n) function for pow(x, y)
func RecursivePower1(n uint, power uint) uint {
if power == 0 {
return 1
} else if power%2 == 0 {
return RecursivePower1(n, power/2) * RecursivePower1(n, power/2)
} else {
return n * RecursivePower1(n, power/2) * RecursivePower1(n, power/2)
}
}
================================================
FILE: math/power/fastexponent_test.go
================================================
package power
import "testing"
var testCases = []struct {
name string
base uint
power uint
expected uint
}{
{"0^2", 0, 2, 0},
{"2^0", 2, 0, 1},
{"2^3", 2, 3, 8},
{"8^3", 8, 3, 512},
{"10^5", 10, 5, 100000},
}
func TestIterativePower(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := IterativePower(tc.base, tc.power)
if actual != tc.expected {
t.Errorf("Expected %d to the power of %d to be: %d, but got: %d", tc.base, tc.power, tc.expected, actual)
}
})
}
}
func TestRecursivePower(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := RecursivePower(tc.base, tc.power)
if actual != tc.expected {
t.Errorf("Expected %d to the power of %d to be: %d, but got: %d", tc.base, tc.power, tc.expected, actual)
}
})
}
}
func TestRecursivePower1(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := RecursivePower1(tc.base, tc.power)
if actual != tc.expected {
t.Errorf("Expected %d to the power of %d to be: %d, but got: %d", tc.base, tc.power, tc.expected, actual)
}
})
}
}
func BenchmarkIterativePower(b *testing.B) {
for i := 0; i < b.N; i++ {
IterativePower(10, 5)
}
}
func BenchmarkRecursivePower(b *testing.B) {
for i := 0; i < b.N; i++ {
RecursivePower(10, 5)
}
}
func BenchmarkRecursivePower1(b *testing.B) {
for i := 0; i < b.N; i++ {
RecursivePower1(10, 5)
}
}
================================================
FILE: math/power/powvialogarithm.go
================================================
// powvialogarithm.go
// description: Powers in terms of logarithms
// details:
// implementation of exponentiation using exponent and logarithm, without using loops - [Powers via logarithms wiki](https://en.wikipedia.org/wiki/Exponentiation#Powers_via_logarithms)
// time complexity: O(1)
// space complexity: O(1)
// author(s) [red_byte](https://github.com/i-redbyte)
// see powvialogarithm_test.go
package power
import (
"math"
)
func UsingLog(a float64, b float64) float64 {
var p float64
p = 1
if a < 0 && int(b)&1 != 0 {
p = -1
}
log := math.Log(math.Abs(a))
exp := math.Exp(b * log)
result := exp * p
return math.Round(result)
}
================================================
FILE: math/power/powvialogarithm_test.go
================================================
// powvialogarithm_test.go
// description: Test for UsingLog
// author(s) [red_byte](https://github.com/i-redbyte)
// see powvialogarithm.go
package power
import "testing"
func TestUsingLog(t *testing.T) {
var tests = []struct {
name string
base float64
power float64
expected float64
}{
{"0^0", 99, 1, 99},
{"-3^9", -3, 9, -19683},
{"0^2", 0, 2, 0},
{"2^0", 2, 0, 1},
{"2^3", 2, 3, 8},
{"8^3", 8, 3, 512},
{"11^11", 11, 11, 285311670611},
{"5^5", 5, 5, 3125},
{"-7^2", -7, 2, 49},
{"-6^3", -6, 3, -216},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := UsingLog(tc.base, tc.power)
t.Log(result)
if result != tc.expected {
t.Errorf("Expected %.2f to the power of %.2f to be: %.2f, but got: %.2f", tc.base, tc.power, tc.expected, result)
}
})
}
}
func BenchmarkUsingLog(b *testing.B) {
for i := 0; i < b.N; i++ {
UsingLog(10, 5)
}
}
================================================
FILE: math/prime/millerrabintest.go
================================================
// This file implements two versions of the Miller-Rabin primality test.
// One of the implementations is deterministic and the other is probabilistic.
// The Miller-Rabin test is one of the simplest and fastest known primality
// tests and is widely used.
// time complexity: O(k * log(n)^3)
// space complexity: O(1)
// Authors:
// [Taj](https://github.com/tjgurwara99)
// [Rak](https://github.com/raklaptudirm)
package prime
import (
"math/rand"
"github.com/TheAlgorithms/Go/math/modular"
)
// formatNum accepts a number and returns the
// odd number d such that num = 2^s * d + 1
func formatNum(num int64) (d int64, s int64) {
d = num - 1
for num%2 == 0 {
d /= 2
s++
}
return
}
// isTrivial checks if num's primality is easy to determine.
// If it is, it returns true and num's primality. Otherwise
// it returns false and false.
func isTrivial(num int64) (prime bool, trivial bool) {
if num <= 4 {
// 2 and 3 are primes
prime = num == 2 || num == 3
trivial = true
} else {
prime = false
// number is trivial prime if
// it is divisible by 2
trivial = num%2 == 0
}
return
}
// MillerTest tests whether num is a strong probable prime to a witness.
// Formally: a^d ≡ 1 (mod n) or a^(2^r * d) ≡ -1 (mod n), 0 <= r <= s
func MillerTest(num, witness int64) (bool, error) {
d, _ := formatNum(num)
res, err := modular.Exponentiation(witness, d, num)
if err != nil {
return false, err
}
// miller conditions checks
if res == 1 || res == num-1 {
return true, nil
}
for d != num-1 {
res = (res * res) % num
d *= 2
if res == 1 {
return false, nil
}
if res == num-1 {
return true, nil
}
}
return false, nil
}
// MillerRandomTest This is the intermediate step that repeats within the
// miller rabin primality test for better probabilitic chances of
// receiving the correct result with random witnesses.
func MillerRandomTest(num int64) (bool, error) {
random := rand.Int63n(num-2) + 2
return MillerTest(num, random)
}
// MillerTestMultiple is like MillerTest but runs the test for multiple
// witnesses.
func MillerTestMultiple(num int64, witnesses ...int64) (bool, error) {
for _, witness := range witnesses {
prime, err := MillerTest(num, witness)
if err != nil {
return false, err
}
if !prime {
return false, nil
}
}
return true, nil
}
// MillerRabinProbabilistic is a probabilistic test for primality
// of an integer based of the algorithm devised by Miller and Rabin.
func MillerRabinProbabilistic(num, rounds int64) (bool, error) {
if prime, trivial := isTrivial(num); trivial {
// num is a trivial number
return prime, nil
}
for i := int64(0); i < rounds; i++ {
val, err := MillerRandomTest(num)
if err != nil {
return false, err
}
if !val {
return false, nil
}
}
return true, nil
}
// MillerRabinDeterministic is a Deterministic version of the Miller-Rabin
// test, which returns correct results for all valid int64 numbers.
func MillerRabinDeterministic(num int64) (bool, error) {
if prime, trivial := isTrivial(num); trivial {
// num is a trivial number
return prime, nil
}
switch {
case num < 2047:
// witness 2 can determine the primality of any number less than 2047
return MillerTest(num, 2)
case num < 1_373_653:
// witnesses 2 and 3 can determine the primality
// of any number less than 1,373,653
return MillerTestMultiple(num, 2, 3)
case num < 9_080_191:
// witnesses 31 and 73 can determine the primality
// of any number less than 9,080,191
return MillerTestMultiple(num, 31, 73)
case num < 25_326_001:
// witnesses 2, 3, and 5 can determine the
// primality of any number less than 25,326,001
return MillerTestMultiple(num, 2, 3, 5)
case num < 1_122_004_669_633:
// witnesses 2, 13, 23, and 1,662,803 can determine the
// primality of any number less than 1,122,004,669,633
return MillerTestMultiple(num, 2, 13, 23, 1_662_803)
case num < 2_152_302_898_747:
// witnesses 2, 3, 5, 7, and 11 can determine the primality
// of any number less than 2,152,302,898,747
return MillerTestMultiple(num, 2, 3, 5, 7, 11)
case num < 341_550_071_728_321:
// witnesses 2, 3, 5, 7, 11, 13, and 17 can determine the
// primality of any number less than 341,550,071,728,321
return MillerTestMultiple(num, 2, 3, 5, 7, 11, 13, 17)
default:
// witnesses 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, and 37 can determine
// the primality of any number less than 318,665,857,834,031,151,167,461
// which is well above the max int64 9,223,372,036,854,775,807
return MillerTestMultiple(num, 2, 3, 5, 7, 11, 13, 17, 19, 23, 31, 37)
}
}
================================================
FILE: math/prime/prime_test.go
================================================
package prime
import (
"fmt"
"testing"
)
var primeList = []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127}
var testLimit = 127
type primalityTest func(int64) bool
func primalityTestTestingHelper(t *testing.T, name string, f primalityTest) {
arrayIndex := 0
for i := 1; i <= testLimit; i++ {
isPrime := i == primeList[arrayIndex]
testName := fmt.Sprintf("%s(%d)", name, i)
t.Run(testName, func(t *testing.T) {
result := f(int64(i))
if isPrime {
arrayIndex++
}
if result != isPrime {
t.Errorf("%d: %s function returned %v\n", i, name, result)
}
})
}
}
func primalityTestBenchmarkHelper(b *testing.B, f primalityTest) {
for i := 0; i < b.N; i++ {
f(104729)
}
}
// Miller-Rabin Probabilistic test
func millerRabinProbabilisticTester(n int64) bool {
result, err := MillerRabinProbabilistic(n, 40)
if err != nil {
panic(err)
}
return result
}
func TestMillerRabinProbabilistic(t *testing.T) {
primalityTestTestingHelper(t, "Miller-Rabin Probabilistic", millerRabinProbabilisticTester)
}
func BenchmarkMillerRabinProbabilistic(b *testing.B) {
primalityTestBenchmarkHelper(b, millerRabinProbabilisticTester)
}
// Miller-Rabin deterministic test
func millerRabinDeterministicTester(n int64) bool {
result, err := MillerRabinDeterministic(n)
if err != nil {
panic(err)
}
return result
}
func TestMillerRabinDeterministic(t *testing.T) {
primalityTestTestingHelper(t, "Miller-Rabin Deterministic", millerRabinDeterministicTester)
}
func BenchmarkMillerRabinDeterministic(b *testing.B) {
primalityTestBenchmarkHelper(b, millerRabinDeterministicTester)
}
// Trial Division test
func TestTrialDivision(t *testing.T) {
primalityTestTestingHelper(t, "Trial Division", TrialDivision)
}
func BenchmarkTrialDivision(b *testing.B) {
primalityTestBenchmarkHelper(b, TrialDivision)
}
// Trial Division (optimized)
func TestOptimizedTrialDivision(t *testing.T) {
primalityTestTestingHelper(t, "Trial Division (optimized)", OptimizedTrialDivision)
}
func BenchmarkOptimizedTrialDivision(b *testing.B) {
primalityTestBenchmarkHelper(b, OptimizedTrialDivision)
}
================================================
FILE: math/prime/primecheck.go
================================================
package prime
// A primality test is an algorithm for determining whether an input number is prime. Among other
// fields of mathematics, it is used for cryptography. Unlike integer factorization, primality
// tests do not generally give prime factors, only stating whether the input number is prime or not.
// time complexity: O(sqrt(n))
// space complexity: O(1)
// Source - Wikipedia https://en.wikipedia.org/wiki/Primality_test
// TrialDivision tests whether a number is prime by trying to divide it by the numbers less than it.
func TrialDivision(n int64) bool {
if n < 2 {
return false
}
for i := int64(2); i < n; i++ {
if n%i == 0 {
return false
}
}
return true
}
// OptimizedTrialDivision checks primality of an integer using an optimized trial division method.
// The optimizations include not checking divisibility by the even numbers and only checking up to
// the square root of the given number.
func OptimizedTrialDivision(n int64) bool {
// 0 and 1 are not prime
if n < 2 {
return false
}
// 2 and 3 are prime
if n < 4 {
return true
}
// all numbers divisible by 2 except 2 are not prime
if n%2 == 0 {
return false
}
for i := int64(3); i*i <= n; i += 2 {
if n%i == 0 {
return false
}
}
return true
}
================================================
FILE: math/prime/primefactorization.go
================================================
// primefactorization.go
// description: Prime factorization of a number
// time complexity: O(sqrt(n))
// space complexity: O(sqrt(n))
package prime
// Factorize is a function that computes the exponents
// of each prime in the prime factorization of n
func Factorize(n int64) map[int64]int64 {
result := make(map[int64]int64)
for i := int64(2); i*i <= n; i += 1 {
for {
if n%i != 0 {
break
}
result[i] += 1
n /= i
}
}
if n > 1 {
result[n] += 1
}
return result
}
================================================
FILE: math/prime/primefactorization_test.go
================================================
package prime
import (
"reflect"
"testing"
)
func TestFactorize(t *testing.T) {
var tests = []struct {
n int64
expected map[int64]int64
}{
{4, map[int64]int64{2: 2}},
{5, map[int64]int64{5: 1}},
{7, map[int64]int64{7: 1}},
{10, map[int64]int64{2: 1, 5: 1}},
{999, map[int64]int64{3: 3, 37: 1}},
{999999999999878, map[int64]int64{2: 1, 19: 1, 26315789473681: 1}},
}
for _, test := range tests {
result := Factorize(test.n)
t.Log(test.n, " ", result)
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.expected, result)
}
}
}
func BenchmarkFactorize(b *testing.B) {
for i := 0; i < b.N; i++ {
Factorize(1000000007)
}
}
================================================
FILE: math/prime/sieve.go
================================================
// sieve.go
// description: Algorithms for generating prime numbers efficiently
// author(s) [Taj](https://github.com/tjgurwara99)
// see sieve_test.go
package prime
// Generate generates the sequence of integers starting at 2 and sends it to the channel `ch`
func GenerateChannel(ch chan<- int) {
for i := 2; ; i++ {
ch <- i
}
}
// Sieve Sieving the numbers that are not prime from the channel - basically removing them from the channels
func Sieve(in <-chan int, out chan<- int, prime int) {
for {
i := <-in
if i%prime != 0 {
out <- i
}
}
}
// Generate returns a int slice of prime numbers up to the limit
func Generate(limit int) []int {
var primes []int
ch := make(chan int)
go GenerateChannel(ch)
for i := 0; i < limit; i++ {
primes = append(primes, <-ch)
ch1 := make(chan int)
go Sieve(ch, ch1, primes[i])
ch = ch1
}
return primes
}
================================================
FILE: math/prime/sieve2.go
================================================
/* sieve2.go - Sieve of Eratosthenes
* Algorithm to generate prime numbers up to a limit
* time complexity: O(n log log n)
* space complexity: O(n)
* Author: ddaniel27
*/
package prime
func SieveEratosthenes(limit int) []int {
primes := make([]int, 0)
sieve := make([]int, limit+1) // make a slice of size limit+1
for i := 2; i <= limit; i++ {
if sieve[i] == 0 { // if the number is not marked as composite
primes = append(primes, i) // add it to the list of primes
for j := i * i; j <= limit; j += i { // mark all multiples of i as composite
sieve[j] = 1
}
}
}
return primes
}
================================================
FILE: math/prime/sieve2_test.go
================================================
package prime_test
import (
"reflect"
"testing"
"github.com/TheAlgorithms/Go/math/prime"
)
func TestSieveEratosthenes(t *testing.T) {
tests := []struct {
name string
limit int
want []int
}{
{
name: "First 10 primes test",
limit: 30,
want: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29},
},
{
name: "First 20 primes test",
limit: 71,
want: []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := prime.SieveEratosthenes(tt.limit)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SieveEratosthenes() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkSieveEratosthenes(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = prime.SieveEratosthenes(10)
}
}
================================================
FILE: math/prime/sieve_test.go
================================================
// sieve_test.go
// description: Testing all the algorithms related to the generation of prime numbers in sieve.go
// author(s) [Taj](https://github.com/tjgurwara99)
// see sieve.go
package prime
import (
"reflect"
"testing"
)
func TestSieve(t *testing.T) {
firstTenPrimes := [10]int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
t.Run("First 10 primes test", func(t *testing.T) {
var testTenPrimes [10]int
ch := make(chan int)
go GenerateChannel(ch)
for i := 0; i < 10; i++ {
testTenPrimes[i] = <-ch
ch1 := make(chan int)
go Sieve(ch, ch1, testTenPrimes[i])
ch = ch1
}
if firstTenPrimes != testTenPrimes {
t.Errorf("The first 10 primes do not match")
}
})
}
func TestGeneratePrimes(t *testing.T) {
firstTenPrimes := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
t.Run("Testing GeneratePrimes Function", func(t *testing.T) {
testPrimes := Generate(10)
if !reflect.DeepEqual(firstTenPrimes, testPrimes) {
t.Fatal("GeneratePrimes function failed")
}
})
}
func BenchmarkSieve10(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Generate(10)
}
}
================================================
FILE: math/prime/twin.go
================================================
// twin.go
// description: Returns Twin Prime of n
// details:
// For any integer n, twin prime is (n + 2)
// if and only if both n and (n + 2) both are prime
// wikipedia: https://en.wikipedia.org/wiki/Twin_prime
// time complexity: O(log n)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see twin_test.go
package prime
// This function returns twin prime for given number
// returns (n + 2) if both n and (n + 2) are prime
// -1 otherwise
func Twin(n int) (int, bool) {
if OptimizedTrialDivision(int64(n)) && OptimizedTrialDivision(int64(n+2)) {
return n + 2, true
}
return -1, false
}
================================================
FILE: math/prime/twin_test.go
================================================
// twin_test.go
// description: Returns Twin Prime of n
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see twin.go
package prime_test
import (
"testing"
"github.com/TheAlgorithms/Go/math/prime"
)
func TestTwin(t *testing.T) {
var tests = []struct {
name string
n int
expectedValue int
hasTwin bool
}{
{"n = 3, should return 5", 3, 5, true},
{"n = 4, should return -1", 4, -1, false},
{"n = 5, should return 7", 5, 7, true},
{"n = 17, should return 19", 17, 19, true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, hasTwin := prime.Twin(test.n)
if result != test.expectedValue || hasTwin != test.hasTwin {
t.Errorf("expected value: %v and %v, got: %v and %v", test.expectedValue, test.hasTwin, result, hasTwin)
}
})
}
}
func BenchmarkTwin(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = prime.Twin(65536)
}
}
================================================
FILE: math/pronicnumber.go
================================================
// pronicnumber.go
// description: Returns true if the number is pronic and false otherwise
// details:
// Pronic number: For any integer n, if there exists integer m
// such that n = m * (m + 1) then n is called a pronic number.
// wikipedia: https://en.wikipedia.org/wiki/Pronic_number
// time complexity: O(1)
// space complexity: O(1)
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see pronicnumber_test.go
package math
import "math"
// PronicNumber returns true if argument passed to the function is pronic and false otherwise.
func PronicNumber(n int) bool {
if n < 0 || n%2 == 1 {
return false
}
x := int(math.Sqrt(float64(n)))
return n == x*(x+1)
}
================================================
FILE: math/pronicnumber_test.go
================================================
// pronicnumber_test.go
// author: Akshay Dubey (https://github.com/itsAkshayDubey)
// see pronicnumber.go
package math_test
import (
"testing"
"github.com/TheAlgorithms/Go/math"
)
func TestPronicNumber(t *testing.T) {
var tests = []struct {
name string
n int
expectedValue bool
}{
{"-12 is not pronic", -12, false},
{"0 is pronic", 0, true},
{"1 is not pronic", 1, false},
{"2 is pronic", 2, true},
{"3 is not pronic", 3, false},
{"4 is not pronic", 4, false},
{"5 is not pronic", 5, false},
{"6 is pronic", 6, true},
{"7 is not pronic", 7, false},
{"8 is not pronic", 8, false},
{"9 is not pronic", 9, false},
{"10 is not pronic", 10, false},
{"11 is not pronic", 11, false},
{"12 is pronic", 12, true},
{"13 is not pronic", 13, false},
{"14 is not pronic", 14, false},
{"15 is not pronic", 15, false},
{"16 is not pronic", 16, false},
{"17 is not pronic", 17, false},
{"18 is not pronic", 18, false},
{"19 is not pronic", 19, false},
{"20 is pronic", 20, true},
{"21 is not pronic", 21, false},
{"22 is not pronic", 22, false},
{"23 is not pronic", 23, false},
{"24 is not pronic", 24, false},
{"25 is not pronic", 25, false},
{"26 is not pronic", 26, false},
{"27 is not pronic", 27, false},
{"28 is not pronic", 28, false},
{"29 is not pronic", 29, false},
{"30 is pronic", 30, true},
{"31 is not pronic", 31, false},
{"32 is not pronic", 32, false},
{"33 is not pronic", 33, false},
{"34 is not pronic", 34, false},
{"35 is not pronic", 35, false},
{"36 is not pronic", 36, false},
{"37 is not pronic", 37, false},
{"38 is not pronic", 38, false},
{"39 is not pronic", 39, false},
{"40 is not pronic", 40, false},
{"41 is not pronic", 41, false},
{"42 is pronic", 42, true},
{"43 is not pronic", 43, false},
{"44 is not pronic", 44, false},
{"45 is not pronic", 45, false},
{"46 is not pronic", 46, false},
{"47 is not pronic", 47, false},
{"48 is not pronic", 48, false},
{"49 is not pronic", 49, false},
{"50 is not pronic", 50, false},
{"51 is not pronic", 51, false},
{"52 is not pronic", 52, false},
{"53 is not pronic", 53, false},
{"54 is not pronic", 54, false},
{"55 is not pronic", 55, false},
{"56 is pronic", 56, true},
{"57 is not pronic", 57, false},
{"58 is not pronic", 58, false},
{"59 is not pronic", 59, false},
{"60 is not pronic", 60, false},
{"61 is not pronic", 61, false},
{"62 is not pronic", 62, false},
{"63 is not pronic", 63, false},
{"64 is not pronic", 64, false},
{"65 is not pronic", 65, false},
{"66 is not pronic", 66, false},
{"67 is not pronic", 67, false},
{"68 is not pronic", 68, false},
{"69 is not pronic", 69, false},
{"70 is not pronic", 70, false},
{"71 is not pronic", 71, false},
{"72 is pronic", 72, true},
{"73 is not pronic", 73, false},
{"74 is not pronic", 74, false},
{"75 is not pronic", 75, false},
{"76 is not pronic", 76, false},
{"77 is not pronic", 77, false},
{"78 is not pronic", 78, false},
{"79 is not pronic", 79, false},
{"80 is not pronic", 80, false},
{"81 is not pronic", 81, false},
{"82 is not pronic", 82, false},
{"83 is not pronic", 83, false},
{"84 is not pronic", 84, false},
{"85 is not pronic", 85, false},
{"86 is not pronic", 86, false},
{"87 is not pronic", 87, false},
{"88 is not pronic", 88, false},
{"89 is not pronic", 89, false},
{"90 is pronic", 90, true},
{"91 is not pronic", 91, false},
{"92 is not pronic", 92, false},
{"93 is not pronic", 93, false},
{"94 is not pronic", 94, false},
{"95 is not pronic", 95, false},
{"96 is not pronic", 96, false},
{"97 is not pronic", 97, false},
{"98 is not pronic", 98, false},
{"99 is not pronic", 99, false},
{"100 is not pronic", 100, false},
{"101 is not pronic", 101, false},
{"102 is not pronic", 102, false},
{"103 is not pronic", 103, false},
{"104 is not pronic", 104, false},
{"105 is not pronic", 105, false},
{"106 is not pronic", 106, false},
{"107 is not pronic", 107, false},
{"108 is not pronic", 108, false},
{"109 is not pronic", 109, false},
{"110 is pronic", 110, true},
{"111 is not pronic", 111, false},
{"112 is not pronic", 112, false},
{"113 is not pronic", 113, false},
{"114 is not pronic", 114, false},
{"115 is not pronic", 115, false},
{"116 is not pronic", 116, false},
{"117 is not pronic", 117, false},
{"118 is not pronic", 118, false},
{"119 is not pronic", 119, false},
{"120 is not pronic", 120, false},
{"121 is not pronic", 121, false},
{"122 is not pronic", 122, false},
{"123 is not pronic", 123, false},
{"124 is not pronic", 124, false},
{"125 is not pronic", 125, false},
{"126 is not pronic", 126, false},
{"127 is not pronic", 127, false},
{"128 is not pronic", 128, false},
{"129 is not pronic", 129, false},
{"130 is not pronic", 130, false},
{"131 is not pronic", 131, false},
{"132 is pronic", 132, true},
{"133 is not pronic", 133, false},
{"134 is not pronic", 134, false},
{"135 is not pronic", 135, false},
{"136 is not pronic", 136, false},
{"137 is not pronic", 137, false},
{"138 is not pronic", 138, false},
{"139 is not pronic", 139, false},
{"140 is not pronic", 140, false},
{"141 is not pronic", 141, false},
{"142 is not pronic", 142, false},
{"143 is not pronic", 143, false},
{"144 is not pronic", 144, false},
{"145 is not pronic", 145, false},
{"146 is not pronic", 146, false},
{"147 is not pronic", 147, false},
{"148 is not pronic", 148, false},
{"149 is not pronic", 149, false},
{"150 is not pronic", 150, false},
{"151 is not pronic", 151, false},
{"152 is not pronic", 152, false},
{"153 is not pronic", 153, false},
{"154 is not pronic", 154, false},
{"155 is not pronic", 155, false},
{"156 is pronic", 156, true},
{"157 is not pronic", 157, false},
{"158 is not pronic", 158, false},
{"159 is not pronic", 159, false},
{"160 is not pronic", 160, false},
{"161 is not pronic", 161, false},
{"162 is not pronic", 162, false},
{"163 is not pronic", 163, false},
{"164 is not pronic", 164, false},
{"165 is not pronic", 165, false},
{"166 is not pronic", 166, false},
{"167 is not pronic", 167, false},
{"168 is not pronic", 168, false},
{"169 is not pronic", 169, false},
{"170 is not pronic", 170, false},
{"171 is not pronic", 171, false},
{"172 is not pronic", 172, false},
{"173 is not pronic", 173, false},
{"174 is not pronic", 174, false},
{"175 is not pronic", 175, false},
{"176 is not pronic", 176, false},
{"177 is not pronic", 177, false},
{"178 is not pronic", 178, false},
{"179 is not pronic", 179, false},
{"180 is not pronic", 180, false},
{"181 is not pronic", 181, false},
{"182 is pronic", 182, true},
{"183 is not pronic", 183, false},
{"184 is not pronic", 184, false},
{"185 is not pronic", 185, false},
{"186 is not pronic", 186, false},
{"187 is not pronic", 187, false},
{"188 is not pronic", 188, false},
{"189 is not pronic", 189, false},
{"190 is not pronic", 190, false},
{"191 is not pronic", 191, false},
{"192 is not pronic", 192, false},
{"193 is not pronic", 193, false},
{"194 is not pronic", 194, false},
{"195 is not pronic", 195, false},
{"196 is not pronic", 196, false},
{"197 is not pronic", 197, false},
{"198 is not pronic", 198, false},
{"199 is not pronic", 199, false},
{"200 is not pronic", 200, false},
{"201 is not pronic", 201, false},
{"202 is not pronic", 202, false},
{"203 is not pronic", 203, false},
{"204 is not pronic", 204, false},
{"205 is not pronic", 205, false},
{"206 is not pronic", 206, false},
{"207 is not pronic", 207, false},
{"208 is not pronic", 208, false},
{"209 is not pronic", 209, false},
{"210 is pronic", 210, true},
{"211 is not pronic", 211, false},
{"212 is not pronic", 212, false},
{"213 is not pronic", 213, false},
{"214 is not pronic", 214, false},
{"215 is not pronic", 215, false},
{"216 is not pronic", 216, false},
{"217 is not pronic", 217, false},
{"218 is not pronic", 218, false},
{"219 is not pronic", 219, false},
{"220 is not pronic", 220, false},
{"221 is not pronic", 221, false},
{"222 is not pronic", 222, false},
{"223 is not pronic", 223, false},
{"224 is not pronic", 224, false},
{"225 is not pronic", 225, false},
{"226 is not pronic", 226, false},
{"227 is not pronic", 227, false},
{"228 is not pronic", 228, false},
{"229 is not pronic", 229, false},
{"230 is not pronic", 230, false},
{"231 is not pronic", 231, false},
{"232 is not pronic", 232, false},
{"233 is not pronic", 233, false},
{"234 is not pronic", 234, false},
{"235 is not pronic", 235, false},
{"236 is not pronic", 236, false},
{"237 is not pronic", 237, false},
{"238 is not pronic", 238, false},
{"239 is not pronic", 239, false},
{"240 is pronic", 240, true},
{"241 is not pronic", 241, false},
{"242 is not pronic", 242, false},
{"243 is not pronic", 243, false},
{"244 is not pronic", 244, false},
{"245 is not pronic", 245, false},
{"246 is not pronic", 246, false},
{"247 is not pronic", 247, false},
{"248 is not pronic", 248, false},
{"249 is not pronic", 249, false},
{"250 is not pronic", 250, false},
{"251 is not pronic", 251, false},
{"252 is not pronic", 252, false},
{"253 is not pronic", 253, false},
{"254 is not pronic", 254, false},
{"255 is not pronic", 255, false},
{"256 is not pronic", 256, false},
{"257 is not pronic", 257, false},
{"258 is not pronic", 258, false},
{"259 is not pronic", 259, false},
{"260 is not pronic", 260, false},
{"261 is not pronic", 261, false},
{"262 is not pronic", 262, false},
{"263 is not pronic", 263, false},
{"264 is not pronic", 264, false},
{"265 is not pronic", 265, false},
{"266 is not pronic", 266, false},
{"267 is not pronic", 267, false},
{"268 is not pronic", 268, false},
{"269 is not pronic", 269, false},
{"270 is not pronic", 270, false},
{"271 is not pronic", 271, false},
{"272 is pronic", 272, true},
{"273 is not pronic", 273, false},
{"274 is not pronic", 274, false},
{"275 is not pronic", 275, false},
{"276 is not pronic", 276, false},
{"277 is not pronic", 277, false},
{"278 is not pronic", 278, false},
{"279 is not pronic", 279, false},
{"280 is not pronic", 280, false},
{"281 is not pronic", 281, false},
{"282 is not pronic", 282, false},
{"283 is not pronic", 283, false},
{"284 is not pronic", 284, false},
{"285 is not pronic", 285, false},
{"286 is not pronic", 286, false},
{"287 is not pronic", 287, false},
{"288 is not pronic", 288, false},
{"289 is not pronic", 289, false},
{"290 is not pronic", 290, false},
{"291 is not pronic", 291, false},
{"292 is not pronic", 292, false},
{"293 is not pronic", 293, false},
{"294 is not pronic", 294, false},
{"295 is not pronic", 295, false},
{"296 is not pronic", 296, false},
{"297 is not pronic", 297, false},
{"298 is not pronic", 298, false},
{"299 is not pronic", 299, false},
{"300 is not pronic", 300, false},
{"301 is not pronic", 301, false},
{"302 is not pronic", 302, false},
{"303 is not pronic", 303, false},
{"304 is not pronic", 304, false},
{"305 is not pronic", 305, false},
{"306 is pronic", 306, true},
{"307 is not pronic", 307, false},
{"308 is not pronic", 308, false},
{"309 is not pronic", 309, false},
{"310 is not pronic", 310, false},
{"311 is not pronic", 311, false},
{"312 is not pronic", 312, false},
{"313 is not pronic", 313, false},
{"314 is not pronic", 314, false},
{"315 is not pronic", 315, false},
{"316 is not pronic", 316, false},
{"317 is not pronic", 317, false},
{"318 is not pronic", 318, false},
{"319 is not pronic", 319, false},
{"320 is not pronic", 320, false},
{"321 is not pronic", 321, false},
{"322 is not pronic", 322, false},
{"323 is not pronic", 323, false},
{"324 is not pronic", 324, false},
{"325 is not pronic", 325, false},
{"326 is not pronic", 326, false},
{"327 is not pronic", 327, false},
{"328 is not pronic", 328, false},
{"329 is not pronic", 329, false},
{"330 is not pronic", 330, false},
{"331 is not pronic", 331, false},
{"332 is not pronic", 332, false},
{"333 is not pronic", 333, false},
{"334 is not pronic", 334, false},
{"335 is not pronic", 335, false},
{"336 is not pronic", 336, false},
{"337 is not pronic", 337, false},
{"338 is not pronic", 338, false},
{"339 is not pronic", 339, false},
{"340 is not pronic", 340, false},
{"341 is not pronic", 341, false},
{"342 is pronic", 342, true},
{"343 is not pronic", 343, false},
{"344 is not pronic", 344, false},
{"345 is not pronic", 345, false},
{"346 is not pronic", 346, false},
{"347 is not pronic", 347, false},
{"348 is not pronic", 348, false},
{"349 is not pronic", 349, false},
{"350 is not pronic", 350, false},
{"351 is not pronic", 351, false},
{"352 is not pronic", 352, false},
{"353 is not pronic", 353, false},
{"354 is not pronic", 354, false},
{"355 is not pronic", 355, false},
{"356 is not pronic", 356, false},
{"357 is not pronic", 357, false},
{"358 is not pronic", 358, false},
{"359 is not pronic", 359, false},
{"360 is not pronic", 360, false},
{"361 is not pronic", 361, false},
{"362 is not pronic", 362, false},
{"363 is not pronic", 363, false},
{"364 is not pronic", 364, false},
{"365 is not pronic", 365, false},
{"366 is not pronic", 366, false},
{"367 is not pronic", 367, false},
{"368 is not pronic", 368, false},
{"369 is not pronic", 369, false},
{"370 is not pronic", 370, false},
{"371 is not pronic", 371, false},
{"372 is not pronic", 372, false},
{"373 is not pronic", 373, false},
{"374 is not pronic", 374, false},
{"375 is not pronic", 375, false},
{"376 is not pronic", 376, false},
{"377 is not pronic", 377, false},
{"378 is not pronic", 378, false},
{"379 is not pronic", 379, false},
{"380 is pronic", 380, true},
{"381 is not pronic", 381, false},
{"382 is not pronic", 382, false},
{"383 is not pronic", 383, false},
{"384 is not pronic", 384, false},
{"385 is not pronic", 385, false},
{"386 is not pronic", 386, false},
{"387 is not pronic", 387, false},
{"388 is not pronic", 388, false},
{"389 is not pronic", 389, false},
{"390 is not pronic", 390, false},
{"391 is not pronic", 391, false},
{"392 is not pronic", 392, false},
{"393 is not pronic", 393, false},
{"394 is not pronic", 394, false},
{"395 is not pronic", 395, false},
{"396 is not pronic", 396, false},
{"397 is not pronic", 397, false},
{"398 is not pronic", 398, false},
{"399 is not pronic", 399, false},
{"400 is not pronic", 400, false},
{"2147441940 is pronic", 2147441940, true},
{"9223372033963249500 is pronic", 9223372033963249500, true},
{"9223372033963249664 is not pronic", 9223372033963249664, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := math.PronicNumber(test.n)
if result != test.expectedValue {
t.Errorf("expected value: %v, got: %v", test.expectedValue, result)
}
})
}
}
func BenchmarkPronicNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
math.PronicNumber(65536)
}
}
================================================
FILE: math/pythagoras/pythagoras.go
================================================
package pythagoras
import (
"math"
)
// Vector defines a tuple with 3 values in 3d-space
type Vector struct {
x float64
y float64
z float64
}
// Distance calculates the distance between to vectors with the Pythagoras theorem
func Distance(a, b Vector) float64 {
res := math.Pow(b.x-a.x, 2.0) + math.Pow(b.y-a.y, 2.0) + math.Pow(b.z-a.z, 2.0)
return math.Sqrt(res)
}
================================================
FILE: math/pythagoras/pythagoras_test.go
================================================
package pythagoras
import (
"math"
"testing"
)
// TableDrivenTest for checking multiple values against our Test Function
var distanceTest = []struct {
name string
v1 Vector
v2 Vector
res float64
}{
{"random negative vector", Vector{2, -1, 7}, Vector{1, -3, 5}, 3.0},
{"random wide vectors", Vector{4, 10, 9}, Vector{4, 3, 5}, 8.06},
{"random wide vectors", Vector{8, 5, 5}, Vector{1, 1, 12}, 10.67},
{"random short vectors", Vector{1, 1, 1}, Vector{2, 2, 2}, 1.73},
}
// TestDistance tests the Function Distance with 2 vectors
func TestDistance(t *testing.T) {
t.Parallel() // marks TestDistance as capable of running in parallel with other tests
for _, tt := range distanceTest {
tt := tt // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
res := Distance(tt.v1, tt.v2) // Calculate result for
roundRes := (math.Floor(res*100) / 100) // Round to 2 decimal places because we can't compare an infinite number of places
// Check result
if roundRes != tt.res {
t.Errorf("Distance(%v, %v) = %f, expected %f",
tt.v1, tt.v2, roundRes, tt.res)
}
})
}
}
================================================
FILE: math/sin.go
================================================
// author(s) [red_byte](https://github.com/i-redbyte)
// see sin_test.go
package math
import "math"
// Sin returns the sine of the radian argument x. [See more](https://en.wikipedia.org/wiki/Sine_and_cosine)
func Sin(x float64) float64 {
return Cos((math.Pi / 2) - x)
}
================================================
FILE: math/sin_test.go
================================================
package math_test
import (
algmath "github.com/TheAlgorithms/Go/math"
stdmath "math"
"testing"
)
func TestSin(t *testing.T) {
tests := []struct {
name string
n float64
want float64
}{
{"sin(0)", 0, 0},
{"sin(3π/2)", (3 * stdmath.Pi) / 2, -1},
{"sin(π/2)", stdmath.Pi / 2, 1},
{"sin(π/6)", stdmath.Pi / 6, 0.5},
{"sin(90)", 90, 0.893996663600558},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := algmath.Sin(test.n)
if stdmath.Abs(got-test.want) >= epsilon {
t.Errorf("Sin() = %v, want %v", got, test.want)
t.Errorf("MATH Sin() = %v", stdmath.Sin(test.n))
}
})
}
}
func BenchmarkSin(b *testing.B) {
for i := 0; i < b.N; i++ {
algmath.Sin(180)
}
}
// BenchmarkMathSin is slower because the standard library `math.Sin` calculates a more accurate value.
func BenchmarkMathSin(b *testing.B) {
for i := 0; i < b.N; i++ {
stdmath.Sin(180)
}
}
================================================
FILE: other/doc.go
================================================
// Package other is dedicated to algorithms that
// do not quite fit into any of the other subpackages in this repository.
package other
================================================
FILE: other/maxsubarraysum/maxsubarraysum.go
================================================
/* O(n) solution, for calculating
maximum contiguous sum in the given array. */
// Package maxsubarraysum is a package containing a solution to a common
// problem of finding max contiguous sum within a array of ints.
package maxsubarraysum
import (
"github.com/TheAlgorithms/Go/math/max"
)
// MaxSubarraySum returns the maximum subarray sum
func MaxSubarraySum(array []int) int {
var currentMax int
var maxTillNow int
if len(array) != 0 {
currentMax = array[0]
maxTillNow = array[0]
}
for _, v := range array {
currentMax = max.Int(v, currentMax+v)
maxTillNow = max.Int(maxTillNow, currentMax)
}
return maxTillNow
}
================================================
FILE: other/maxsubarraysum/maxsubarraysum_test.go
================================================
package maxsubarraysum
import (
"testing"
)
func TestMaxSubarraySum(t *testing.T) {
testCases := []struct {
name string
slice []int
expected int
}{
{
name: "Empty slice",
slice: []int{},
expected: 0,
},
{
name: "Max is 0",
slice: []int{0, -1, -2, -4, -5},
expected: 0,
},
{
name: "Max is -1",
slice: []int{-1, -3, -2, -5, -7},
expected: -1,
},
{
name: "Max is 7",
slice: []int{-2, -5, 6, 0, -2, 0, -3, 1, 0, 5, -6},
expected: 7,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
if result := MaxSubarraySum(test.slice); result != test.expected {
t.Fatalf("%s\n\tslice: %v, expected: %v, returned: %v", test.name, test.slice, test.expected, result)
}
})
}
}
================================================
FILE: other/nested/nestedbrackets.go
================================================
// Package nested provides functions for testing
// strings proper brackets nesting.
package nested
// IsBalanced returns true if provided input string is properly nested.
//
// Input is a sequence of brackets: '(', ')', '[', ']', '{', '}'.
//
// A sequence of brackets `s` is considered properly nested
// if any of the following conditions are true:
// - `s` is empty;
// - `s` has the form (U) or [U] or {U} where U is a properly nested string;
// - `s` has the form VW where V and W are properly nested strings.
//
// For example, the string "()()[()]" is properly nested but "[(()]" is not.
//
// **Note** Providing characters other then brackets would return false,
// despite brackets sequence in the string. Make sure to filter
// input before usage.
// time complexity: O(n)
// space complexity: O(n)
func IsBalanced(input string) bool {
if len(input) == 0 {
return true
}
if len(input)%2 != 0 {
return false
}
// Brackets such as '{', '[', '(' are valid UTF-8 characters,
// which means that only one byte is required to code them,
// so can be stored as bytes.
var stack []byte
for i := 0; i < len(input); i++ {
if input[i] == '(' || input[i] == '{' || input[i] == '[' {
stack = append(stack, input[i])
} else {
if len(stack) > 0 {
pair := string(stack[len(stack)-1]) + string(input[i])
stack = stack[:len(stack)-1]
if pair != "[]" && pair != "{}" && pair != "()" {
// This means that two types of brackets has
// been mixed together, for example "([)]",
// which makes seuqence invalid by definition.
return false
}
} else {
// This means that closing bracket is encountered
// before opening one, which makes all sequence
// invalid by definition.
return false
}
}
}
// If sequence is properly nested, all elements in stack
// has been paired with closing elements. If even one
// element has not been paired with a closing bracket,
// means that sequence is invalid by definition.
return len(stack) == 0
}
================================================
FILE: other/nested/nestedbrackets_test.go
================================================
package nested
import (
"testing"
)
func TestIsBalancedSimple(t *testing.T) {
input := "{[()]}"
got := IsBalanced(input)
want := true
if got != want {
t.Errorf("\nInput: %s\nGot: %v\nWant: %v\n", input, got, want)
}
}
func TestIsBalancedFalty(t *testing.T) {
input := "{([()]}"
got := IsBalanced(input)
want := false
if got != want {
t.Errorf("\nInput: %s\nGot: %v\nWant: %v\n", input, got, want)
}
}
func TestIsBalancedHandlesEmpty(t *testing.T) {
input := ""
got := IsBalanced(input)
want := true
if got != want {
t.Errorf("\nInput: %s\nGot: %v\nWant: %v\n", input, got, want)
}
}
func TestIsBalancedHandlesOneChar(t *testing.T) {
input := "{"
got := IsBalanced(input)
want := false
if got != want {
t.Errorf("\nInput: %s\nGot: %v\nWant: %v\n", input, got, want)
}
}
func TestIsBalancedHandlesNonBracketsCorrectly(t *testing.T) {
input := "aaaa"
got := IsBalanced(input)
want := false
if got != want {
t.Errorf("\nInput: %s\nGot: %v\nWant: %v\n", input, got, want)
}
}
func TestIsBalancedHandlesOrdering(t *testing.T) {
input := "([)]"
got := IsBalanced(input)
want := false
if got != want {
t.Errorf("\nInput: %s\nGot: %v\nWant: %v\n", input, got, want)
}
}
func BenchmarkIsBalanced(b *testing.B) {
input := "{[()]}"
for i := 0; i < b.N; i++ {
IsBalanced(input)
}
}
================================================
FILE: other/other_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package other
================================================
FILE: other/password/generator.go
================================================
// This program generates a password from a list of possible chars
// You must provide a minimum length and a maximum length
// This length is not fixed if you generate multiple passwords for the same range
// Package password contains functions to help generate random passwords
// time complexity: O(n)
// space complexity: O(n)
package password
import (
"crypto/rand"
"io"
"math/big"
)
// Generate returns a newly generated password
func Generate(minLength int, maxLength int) string {
var chars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+,.?/:;{}[]`~")
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength-minLength)))
if err != nil {
panic(err) // handle this gracefully
}
length.Add(length, big.NewInt(int64(minLength)))
intLength := int(length.Int64())
newPassword := make([]byte, intLength)
randomData := make([]byte, intLength+intLength/4)
charLen := byte(len(chars))
maxrb := byte(256 - (256 % len(chars)))
i := 0
for {
if _, err := io.ReadFull(rand.Reader, randomData); err != nil {
panic(err)
}
for _, c := range randomData {
if c >= maxrb {
continue
}
newPassword[i] = chars[c%charLen]
i++
if i == intLength {
return string(newPassword)
}
}
}
}
================================================
FILE: project_euler/problem_1/problem1.go
================================================
/**
* Problem 1 - Multiples of 3 and 5
*
* @see {@link https://projecteuler.net/problem=1}
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5,
* we get 3, 5, 6 and 9. The sum of these multiples is 23.
* Find the sum of all the multiples of 3 or 5 below 1000.
*
* @author ddaniel27
*/
package problem1
func Problem1(n uint) uint {
sum := uint(0)
for i := uint(1); i < n; i++ {
if i%3 == 0 || i%5 == 0 {
sum += i
}
}
return sum
}
================================================
FILE: project_euler/problem_1/problem1_test.go
================================================
package problem1
import "testing"
// Tests
func TestProblem1_Func(t *testing.T) {
tests := []struct {
name string
threshold uint
want uint
}{
{
name: "Testcase 1 - threshold 10",
threshold: 10,
want: 23,
},
{
name: "Testcase 2 - threshold 100",
threshold: 100,
want: 2318,
},
{
name: "Testcase 3 - threshold 1000",
threshold: 1000,
want: 233168,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem1(tt.threshold)
if n != tt.want {
t.Errorf("Problem1() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem1(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem1(1000)
}
}
================================================
FILE: project_euler/problem_10/problem10.go
================================================
/**
* Problem 10 - Summation of primes
* @see {@link https://projecteuler.net/problem=10}
*
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
* Find the sum of all the primes below two million.
*
* @author ddaniel27
*/
package problem10
import "github.com/TheAlgorithms/Go/math/prime"
func Problem10(n int) uint {
sum := uint(0)
sieve := prime.SieveEratosthenes(n)
for _, v := range sieve {
sum += uint(v)
}
return sum
}
================================================
FILE: project_euler/problem_10/problem10_test.go
================================================
package problem10
import "testing"
// Tests
func TestProblem10_Func(t *testing.T) {
tests := []struct {
name string
input int
want uint
}{
{
name: "Testcase 1 - input 10",
input: 10,
want: 17,
},
{
name: "Testcase 2 - input 2000000",
input: 2000000,
want: 142913828922,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem10(tt.input)
if n != tt.want {
t.Errorf("Problem10() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem10(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem10(2000000)
}
}
================================================
FILE: project_euler/problem_11/problem11.go
================================================
/**
* Problem 11 - Largest product in a grid
* @see {@link https://projecteuler.net/problem=11}
*
* In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
*
* The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
*
* What is the greatest product of four adjacent numbers in the same direction
* (up, down, left, right, or diagonally) in the 20×20 grid?
*
* @author ddaniel27
*/
package problem11
var grid = [20][20]uint{
{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48},
}
func Problem11() uint {
max := uint(0)
for i := 0; i < 20; i++ {
for j := 0; j < 20; j++ {
// Vertical
if i+3 < 20 {
product := grid[i][j] * grid[i+1][j] * grid[i+2][j] * grid[i+3][j]
if product > max {
max = product
}
}
// Horizontal
if j+3 < 20 {
product := grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3]
if product > max {
max = product
}
}
if i+3 < 20 && j+3 < 20 {
// Diagonal
product := grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2] * grid[i+3][j+3]
if product > max {
max = product
}
}
if i+3 < 20 && j-3 >= 0 {
// Diagonal
product := grid[i][j] * grid[i+1][j-1] * grid[i+2][j-2] * grid[i+3][j-3]
if product > max {
max = product
}
}
}
}
return max
}
================================================
FILE: project_euler/problem_11/problem11_test.go
================================================
package problem11
import "testing"
// Tests
func TestProblem11_Func(t *testing.T) {
testCases := []struct {
name string
expected uint
}{
{"Test Case 1", 70600674},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := Problem11()
if actual != tc.expected {
t.Errorf("Expected: %v, but got %v", tc.expected, actual)
}
})
}
}
// Benchmark
func BenchmarkProblem11_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem11()
}
}
================================================
FILE: project_euler/problem_12/problem12.go
================================================
/**
* Problem 12 - Highly divisible triangular number
* @see {@link https://projecteuler.net/problem=12}
*
* The sequence of triangle numbers is generated by adding the natural numbers.
* So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
* The first ten terms would be:
*
* 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
*
* Let us list the factors of the first seven triangle numbers:
*
* 1: 1
* 3: 1,3
* 6: 1,2,3,6
* 10: 1,2,5,10
* 15: 1,3,5,15
* 21: 1,3,7,21
* 28: 1,2,4,7,14,28
*
* We can see that 28 is the first triangle number to have over five divisors.
* What is the value of the first triangle number to have over five hundred divisors?
*
* @author ddaniel27
*/
package problem12
func Problem12(limit uint) uint {
triangle := uint(0)
for i := uint(1); ; i++ {
triangle += i
if numDivisors(triangle) >= limit {
return triangle
}
}
}
func numDivisors(n uint) uint {
divisors := uint(0)
for i := uint(1); i*i <= n; i++ {
if n%i == 0 {
divisors += 2
}
}
return divisors
}
================================================
FILE: project_euler/problem_12/problem12_test.go
================================================
package problem12
import "testing"
func TestProblem12_Func(t *testing.T) {
tests := []struct {
name string
input uint
want uint
}{
{"Test Case 1", 6, 28},
{"Test Case 2", 7, 36},
{"Test Case 3", 11, 120},
{"Test Case 4", 500, 76576500},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := Problem12(tt.input)
if actual != tt.want {
t.Errorf("Expected: %v, but got %v", tt.want, actual)
}
})
}
}
func BenchmarkProblem12_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem12(500)
}
}
================================================
FILE: project_euler/problem_13/problem13.go
================================================
/**
* Problem 13 - Large sum
* @see {@link https://projecteuler.net/problem=13}
*
* Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
*
* @author ddaniel27
*/
package problem13
var numbers = [100]string{
"37107287533902102798797998220837590246510135740250",
"46376937677490009712648124896970078050417018260538",
"74324986199524741059474233309513058123726617309629",
"91942213363574161572522430563301811072406154908250",
"23067588207539346171171980310421047513778063246676",
"89261670696623633820136378418383684178734361726757",
"28112879812849979408065481931592621691275889832738",
"44274228917432520321923589422876796487670272189318",
"47451445736001306439091167216856844588711603153276",
"70386486105843025439939619828917593665686757934951",
"62176457141856560629502157223196586755079324193331",
"64906352462741904929101432445813822663347944758178",
"92575867718337217661963751590579239728245598838407",
"58203565325359399008402633568948830189458628227828",
"80181199384826282014278194139940567587151170094390",
"35398664372827112653829987240784473053190104293586",
"86515506006295864861532075273371959191420517255829",
"71693888707715466499115593487603532921714970056938",
"54370070576826684624621495650076471787294438377604",
"53282654108756828443191190634694037855217779295145",
"36123272525000296071075082563815656710885258350721",
"45876576172410976447339110607218265236877223636045",
"17423706905851860660448207621209813287860733969412",
"81142660418086830619328460811191061556940512689692",
"51934325451728388641918047049293215058642563049483",
"62467221648435076201727918039944693004732956340691",
"15732444386908125794514089057706229429197107928209",
"55037687525678773091862540744969844508330393682126",
"18336384825330154686196124348767681297534375946515",
"80386287592878490201521685554828717201219257766954",
"78182833757993103614740356856449095527097864797581",
"16726320100436897842553539920931837441497806860984",
"48403098129077791799088218795327364475675590848030",
"87086987551392711854517078544161852424320693150332",
"59959406895756536782107074926966537676326235447210",
"69793950679652694742597709739166693763042633987085",
"41052684708299085211399427365734116182760315001271",
"65378607361501080857009149939512557028198746004375",
"35829035317434717326932123578154982629742552737307",
"94953759765105305946966067683156574377167401875275",
"88902802571733229619176668713819931811048770190271",
"25267680276078003013678680992525463401061632866526",
"36270218540497705585629946580636237993140746255962",
"24074486908231174977792365466257246923322810917141",
"91430288197103288597806669760892938638285025333403",
"34413065578016127815921815005561868836468420090470",
"23053081172816430487623791969842487255036638784583",
"11487696932154902810424020138335124462181441773470",
"63783299490636259666498587618221225225512486764533",
"67720186971698544312419572409913959008952310058822",
"95548255300263520781532296796249481641953868218774",
"76085327132285723110424803456124867697064507995236",
"37774242535411291684276865538926205024910326572967",
"23701913275725675285653248258265463092207058596522",
"29798860272258331913126375147341994889534765745501",
"18495701454879288984856827726077713721403798879715",
"38298203783031473527721580348144513491373226651381",
"34829543829199918180278916522431027392251122869539",
"40957953066405232632538044100059654939159879593635",
"29746152185502371307642255121183693803580388584903",
"41698116222072977186158236678424689157993532961922",
"62467957194401269043877107275048102390895523597457",
"23189706772547915061505504953922979530901129967519",
"86188088225875314529584099251203829009407770775672",
"11306739708304724483816533873502340845647058077308",
"82959174767140363198008187129011875491310547126581",
"97623331044818386269515456334926366572897563400500",
"42846280183517070527831839425882145521227251250327",
"55121603546981200581762165212827652751691296897789",
"32238195734329339946437501907836945765883352399886",
"75506164965184775180738168837861091527357929701337",
"62177842752192623401942399639168044983993173312731",
"32924185707147349566916674687634660915035914677504",
"99518671430235219628894890102423325116913619626622",
"73267460800591547471830798392868535206946944540724",
"76841822524674417161514036427982273348055556214818",
"97142617910342598647204516893989422179826088076852",
"87783646182799346313767754307809363333018982642090",
"10848802521674670883215120185883543223812876952786",
"71329612474782464538636993009049310363619763878039",
"62184073572399794223406235393808339651327408011116",
"66627891981488087797941876876144230030984490851411",
"60661826293682836764744779239180335110989069790714",
"85786944089552990653640447425576083659976645795096",
"66024396409905389607120198219976047599490197230297",
"64913982680032973156037120041377903785566085089252",
"16730939319872750275468906903707539413042652315011",
"94809377245048795150954100921645863754710598436791",
"78639167021187492431995700641917969777599028300699",
"15368713711936614952811305876380278410754449733078",
"40789923115535562561142322423255033685442488917353",
"44889911501440648020369068063960672322193204149535",
"41503128880339536053299340368006977710650566631954",
"81234880673210146739058568557934581403627822703280",
"82616570773948327592232845941706525094512325230608",
"22918802058777319719839450180888072429661980811197",
"77158542502016545090413245809786882778948721859617",
"72107838435069186155435662884062257473692284509516",
"20849603980134001723930671666823555245252804609722",
"53503534226472524250874054075591789781264330331690",
}
func Problem13() string {
sum := "0"
for _, n := range numbers {
sum = add(sum, n)
}
return sum[:10]
}
func add(a, b string) string {
if len(a) < len(b) {
a, b = b, a
}
carry := 0
sum := make([]byte, len(a)+1)
for i := 0; i < len(a); i++ {
d := int(a[len(a)-1-i] - '0')
if i < len(b) {
d += int(b[len(b)-1-i] - '0')
}
d += carry
sum[len(sum)-1-i] = byte(d%10) + '0'
carry = d / 10
}
if carry > 0 {
sum[0] = byte(carry) + '0'
} else {
sum = sum[1:]
}
return string(sum)
}
================================================
FILE: project_euler/problem_13/problem13_test.go
================================================
package problem13
import "testing"
func TestProblem13_Func(t *testing.T) {
tests := []struct {
name string
expected string
}{
{"Test Case 1", "5537376230"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := Problem13()
if actual != tt.expected {
t.Errorf("Expected: %v, but got %v", tt.expected, actual)
}
})
}
}
func BenchmarkProblem13_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem13()
}
}
================================================
FILE: project_euler/problem_14/problem14.go
================================================
/**
* Problem 14 - Longest Collatz sequence
* @see {@link https://projecteuler.net/problem=14}
*
* The following iterative sequence is defined for the set of positive integers:
* n → n/2 (n is even)
* n → 3n + 1 (n is odd)
*
* Using the rule above and starting with 13, we generate the following sequence:
* 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
*
* Which starting number, under one million, produces the longest chain?
*
* NOTE: Once the chain starts the terms are allowed to go above one million.
*
* @author ddaniel27
*/
package problem14
type dict map[uint64]uint64
var dictionary = dict{
1: 1,
}
func Problem14(limit uint64) uint64 {
for i := uint64(2); i <= limit; i++ {
weightNextNode(i)
}
var max, maxWeight uint64
for k, v := range dictionary {
if v > maxWeight {
max = k
maxWeight = v
}
}
return max
}
func weightNextNode(current uint64) uint64 {
var next, weight uint64
if current%2 == 0 {
next = current / 2
} else {
next = (3 * current) + 1
}
if v, ok := dictionary[next]; !ok {
weight = weightNextNode(next) + 1
} else {
weight = v + 1
}
dictionary[current] = weight
return weight
}
================================================
FILE: project_euler/problem_14/problem14_test.go
================================================
package problem14
import "testing"
// Tests
func TestProblem14_Func(t *testing.T) {
tests := []struct {
name string
input uint64
want uint64
}{
{"Input 30", 30, 27},
{"Input 1e6", 1e6, 837799},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Problem14(tt.input)
if got != tt.want {
t.Errorf("Problem14() = %v, want %v", got, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem14_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem14(1e6)
}
}
================================================
FILE: project_euler/problem_15/problem15.go
================================================
/**
* Problem 15 - Lattice paths
* @see {@link https://projecteuler.net/problem=15}
*
* Starting in the top left corner of a 2×2 grid,
* and only being able to move to the right and down,
* there are exactly 6 routes to the bottom right corner.
*
* How many such routes are there through a 20×20 grid?
*
* @author ddaniel27
*/
package problem15
import (
"github.com/TheAlgorithms/Go/math/factorial"
)
func Problem15(gridSize int) int {
/**
Author note:
We can solve this problem using combinatorics.
Here is a good blog post that explains the solution:
[link](https://stemhash.com/counting-lattice-paths/)
Btw, I'm not related to the author of the blog post.
After some simplification, we can see that the solution is:
(2n)! / (n!)^2
We can use the factorial package to calculate the factorials.
*/
n := gridSize
numerator, _ := factorial.Iterative(2 * n)
denominator, _ := factorial.Iterative(n)
denominator *= denominator
return numerator / denominator
}
================================================
FILE: project_euler/problem_15/problem15_test.go
================================================
package problem15
import "testing"
// Tests
func TestProblem15_Func(t *testing.T) {
tests := []struct {
name string
input int
want int
}{
{"Input 2", 2, 6},
// This test case is disabled
// because it needs a big integer to run successfully
// and factorial package doesn't support it
// {"Input 20", 20, 137846528820},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Problem15(tt.input)
if got != tt.want {
t.Errorf("Problem15() = %v, want %v", got, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem15_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem15(20)
}
}
================================================
FILE: project_euler/problem_16/problem16.go
================================================
/**
* Problem 16 - Power digit sum
* @see {@link https://projecteuler.net/problem=16}
*
* 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
*
* What is the sum of the digits of the number 2^1000?
*
* @author ddaniel27
*/
package problem16
import (
"math/big"
)
func Problem16(exponent int64) int64 {
var result big.Int
bigTwo := big.NewInt(2)
bigExponent := big.NewInt(exponent)
result.Exp(bigTwo, bigExponent, nil)
resultStr := result.String()
var sum int64
for _, digit := range resultStr {
sum += int64(digit - '0')
}
return sum
}
================================================
FILE: project_euler/problem_16/problem16_test.go
================================================
package problem16
import "testing"
// Tests
func TestProblem16_Func(t *testing.T) {
tests := []struct {
name string
exponent int64
want int64
}{
{"2^15", 15, 26},
{"2^1000", 1000, 1366},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Problem16(tt.exponent); got != tt.want {
t.Errorf("Problem16() = %v, want %v", got, tt.want)
}
})
}
}
// Benchmark
func BenchmarkProblem16_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem16(1000)
}
}
================================================
FILE: project_euler/problem_17/input.go
================================================
/**
* I put this code in a separate file because it is too long.
* Also it took me a lot of time to parsing this input from
* a random html page, so, I don't want to lose it.
*/
package problem17
const INPUT = "One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen Twenty Twenty one Twenty two Twenty three Twenty four Twenty five Twenty six Twenty seven Twenty eight Twenty nine Thirty Thirty one Thirty two Thirty three Thirty four Thirty five Thirty six Thirty seven Thirty eight Thirty nine Forty Forty one Forty two Forty three Forty four Forty five Forty six Forty seven Forty eight Forty nine Fifty Fifty one Fifty two Fifty three Fifty four Fifty five Fifty six Fifty seven Fifty eight Fifty nine Sixty Sixty one Sixty two Sixty three Sixty four Sixty five Sixty six Sixty seven Sixty eight Sixty nine Seventy Seventy one Seventy two Seventy three Seventy four Seventy five Seventy six Seventy seven Seventy eight Seventy nine Eighty Eighty one Eighty two Eighty three Eighty four Eighty five Eighty six Eighty seven Eighty eight Eighty nine Ninety Ninety one Ninety two Ninety three Ninety four Ninety five Ninety six Ninety seven Ninety eight Ninety nine One hundred One hundred and one One hundred and two One hundred and three One hundred and four One hundred and five One hundred and six One hundred and seven One hundred and eight One hundred and nine One hundred and ten One hundred and eleven One hundred and twelve One hundred and thirteen One hundred and fourteen One hundred and fifteen One hundred and sixteen One hundred and seventeen One hundred and eighteen One hundred and nineteen One hundred and twenty One hundred and twenty one One hundred and twenty two One hundred and twenty three One hundred and twenty four One hundred and twenty five One hundred and twenty six One hundred and twenty seven One hundred and twenty eight One hundred and twenty nine One hundred and thirty One hundred and thirty one One hundred and thirty two One hundred and thirty three One hundred and thirty four One hundred and thirty five One hundred and thirty six One hundred and thirty seven One hundred and thirty eight One hundred and thirty nine One hundred and forty One hundred and forty one One hundred and forty two One hundred and forty three One hundred and forty four One hundred and forty five One hundred and forty six One hundred and forty seven One hundred and forty eight One hundred and forty nine One hundred and fifty One hundred and fifty one One hundred and fifty two One hundred and fifty three One hundred and fifty four One hundred and fifty five One hundred and fifty six One hundred and fifty seven One hundred and fifty eight One hundred and fifty nine One hundred and sixty One hundred and sixty one One hundred and sixty two One hundred and sixty three One hundred and sixty four One hundred and sixty five One hundred and sixty six One hundred and sixty seven One hundred and sixty eight One hundred and sixty nine One hundred and seventy One hundred and seventy one One hundred and seventy two One hundred and seventy three One hundred and seventy four One hundred and seventy five One hundred and seventy six One hundred and seventy seven One hundred and seventy eight One hundred and seventy nine One hundred and eighty One hundred and eighty one One hundred and eighty two One hundred and eighty three One hundred and eighty four One hundred and eighty five One hundred and eighty six One hundred and eighty seven One hundred and eighty eight One hundred and eighty nine One hundred and ninety One hundred and ninety one One hundred and ninety two One hundred and ninety three One hundred and ninety four One hundred and ninety five One hundred and ninety six One hundred and ninety seven One hundred and ninety eight One hundred and ninety nine Two hundred Two hundred and one Two hundred and two Two hundred and three Two hundred and four Two hundred and five Two hundred and six Two hundred and seven Two hundred and eight Two hundred and nine Two hundred and ten Two hundred and eleven Two hundred and twelve Two hundred and thirteen Two hundred and fourteen Two hundred and fifteen Two hundred and sixteen Two hundred and seventeen Two hundred and eighteen Two hundred and nineteen Two hundred and twenty Two hundred and twenty one Two hundred and twenty two Two hundred and twenty three Two hundred and twenty four Two hundred and twenty five Two hundred and twenty six Two hundred and twenty seven Two hundred and twenty eight Two hundred and twenty nine Two hundred and thirty Two hundred and thirty one Two hundred and thirty two Two hundred and thirty three Two hundred and thirty four Two hundred and thirty five Two hundred and thirty six Two hundred and thirty seven Two hundred and thirty eight Two hundred and thirty nine Two hundred and forty Two hundred and forty one Two hundred and forty two Two hundred and forty three Two hundred and forty four Two hundred and forty five Two hundred and forty six Two hundred and forty seven Two hundred and forty eight Two hundred and forty nine Two hundred and fifty Two hundred and fifty one Two hundred and fifty two Two hundred and fifty three Two hundred and fifty four Two hundred and fifty five Two hundred and fifty six Two hundred and fifty seven Two hundred and fifty eight Two hundred and fifty nine Two hundred and sixty Two hundred and sixty one Two hundred and sixty two Two hundred and sixty three Two hundred and sixty four Two hundred and sixty five Two hundred and sixty six Two hundred and sixty seven Two hundred and sixty eight Two hundred and sixty nine Two hundred and seventy Two hundred and seventy one Two hundred and seventy two Two hundred and seventy three Two hundred and seventy four Two hundred and seventy five Two hundred and seventy six Two hundred and seventy seven Two hundred and seventy eight Two hundred and seventy nine Two hundred and eighty Two hundred and eighty one Two hundred and eighty two Two hundred and eighty three Two hundred and eighty four Two hundred and eighty five Two hundred and eighty six Two hundred and eighty seven Two hundred and eighty eight Two hundred and eighty nine Two hundred and ninety Two hundred and ninety one Two hundred and ninety two Two hundred and ninety three Two hundred and ninety four Two hundred and ninety five Two hundred and ninety six Two hundred and ninety seven Two hundred and ninety eight Two hundred and ninety nine Three hundred Three hundred and one Three hundred and two Three hundred and three Three hundred and four Three hundred and five Three hundred and six Three hundred and seven Three hundred and eight Three hundred and nine Three hundred and ten Three hundred and eleven Three hundred and twelve Three hundred and thirteen Three hundred and fourteen Three hundred and fifteen Three hundred and sixteen Three hundred and seventeen Three hundred and eighteen Three hundred and nineteen Three hundred and twenty Three hundred and twenty one Three hundred and twenty two Three hundred and twenty three Three hundred and twenty four Three hundred and twenty five Three hundred and twenty six Three hundred and twenty seven Three hundred and twenty eight Three hundred and twenty nine Three hundred and thirty Three hundred and thirty one Three hundred and thirty two Three hundred and thirty three Three hundred and thirty four Three hundred and thirty five Three hundred and thirty six Three hundred and thirty seven Three hundred and thirty eight Three hundred and thirty nine Three hundred and forty Three hundred and forty one Three hundred and forty two Three hundred and forty three Three hundred and forty four Three hundred and forty five Three hundred and forty six Three hundred and forty seven Three hundred and forty eight Three hundred and forty nine Three hundred and fifty Three hundred and fifty one Three hundred and fifty two Three hundred and fifty three Three hundred and fifty four Three hundred and fifty five Three hundred and fifty six Three hundred and fifty seven Three hundred and fifty eight Three hundred and fifty nine Three hundred and sixty Three hundred and sixty one Three hundred and sixty two Three hundred and sixty three Three hundred and sixty four Three hundred and sixty five Three hundred and sixty six Three hundred and sixty seven Three hundred and sixty eight Three hundred and sixty nine Three hundred and seventy Three hundred and seventy one Three hundred and seventy two Three hundred and seventy three Three hundred and seventy four Three hundred and seventy five Three hundred and seventy six Three hundred and seventy seven Three hundred and seventy eight Three hundred and seventy nine Three hundred and eighty Three hundred and eighty one Three hundred and eighty two Three hundred and eighty three Three hundred and eighty four Three hundred and eighty five Three hundred and eighty six Three hundred and eighty seven Three hundred and eighty eight Three hundred and eighty nine Three hundred and ninety Three hundred and ninety one Three hundred and ninety two Three hundred and ninety three Three hundred and ninety four Three hundred and ninety five Three hundred and ninety six Three hundred and ninety seven Three hundred and ninety eight Three hundred and ninety nine Four hundred Four hundred and one Four hundred and two Four hundred and three Four hundred and four Four hundred and five Four hundred and six Four hundred and seven Four hundred and eight Four hundred and nine Four hundred and ten Four hundred and eleven Four hundred and twelve Four hundred and thirteen Four hundred and fourteen Four hundred and fifteen Four hundred and sixteen Four hundred and seventeen Four hundred and eighteen Four hundred and nineteen Four hundred and twenty Four hundred and twenty one Four hundred and twenty two Four hundred and twenty three Four hundred and twenty four Four hundred and twenty five Four hundred and twenty six Four hundred and twenty seven Four hundred and twenty eight Four hundred and twenty nine Four hundred and thirty Four hundred and thirty one Four hundred and thirty two Four hundred and thirty three Four hundred and thirty four Four hundred and thirty five Four hundred and thirty six Four hundred and thirty seven Four hundred and thirty eight Four hundred and thirty nine Four hundred and forty Four hundred and forty one Four hundred and forty two Four hundred and forty three Four hundred and forty four Four hundred and forty five Four hundred and forty six Four hundred and forty seven Four hundred and forty eight Four hundred and forty nine Four hundred and fifty Four hundred and fifty one Four hundred and fifty two Four hundred and fifty three Four hundred and fifty four Four hundred and fifty five Four hundred and fifty six Four hundred and fifty seven Four hundred and fifty eight Four hundred and fifty nine Four hundred and sixty Four hundred and sixty one Four hundred and sixty two Four hundred and sixty three Four hundred and sixty four Four hundred and sixty five Four hundred and sixty six Four hundred and sixty seven Four hundred and sixty eight Four hundred and sixty nine Four hundred and seventy Four hundred and seventy one Four hundred and seventy two Four hundred and seventy three Four hundred and seventy four Four hundred and seventy five Four hundred and seventy six Four hundred and seventy seven Four hundred and seventy eight Four hundred and seventy nine Four hundred and eighty Four hundred and eighty one Four hundred and eighty two Four hundred and eighty three Four hundred and eighty four Four hundred and eighty five Four hundred and eighty six Four hundred and eighty seven Four hundred and eighty eight Four hundred and eighty nine Four hundred and ninety Four hundred and ninety one Four hundred and ninety two Four hundred and ninety three Four hundred and ninety four Four hundred and ninety five Four hundred and ninety six Four hundred and ninety seven Four hundred and ninety eight Four hundred and ninety nine Five hundred Five hundred and one Five hundred and two Five hundred and three Five hundred and four Five hundred and five Five hundred and six Five hundred and seven Five hundred and eight Five hundred and nine Five hundred and ten Five hundred and eleven Five hundred and twelve Five hundred and thirteen Five hundred and fourteen Five hundred and fifteen Five hundred and sixteen Five hundred and seventeen Five hundred and eighteen Five hundred and nineteen Five hundred and twenty Five hundred and twenty one Five hundred and twenty two Five hundred and twenty three Five hundred and twenty four Five hundred and twenty five Five hundred and twenty six Five hundred and twenty seven Five hundred and twenty eight Five hundred and twenty nine Five hundred and thirty Five hundred and thirty one Five hundred and thirty two Five hundred and thirty three Five hundred and thirty four Five hundred and thirty five Five hundred and thirty six Five hundred and thirty seven Five hundred and thirty eight Five hundred and thirty nine Five hundred and forty Five hundred and forty one Five hundred and forty two Five hundred and forty three Five hundred and forty four Five hundred and forty five Five hundred and forty six Five hundred and forty seven Five hundred and forty eight Five hundred and forty nine Five hundred and fifty Five hundred and fifty one Five hundred and fifty two Five hundred and fifty three Five hundred and fifty four Five hundred and fifty five Five hundred and fifty six Five hundred and fifty seven Five hundred and fifty eight Five hundred and fifty nine Five hundred and sixty Five hundred and sixty one Five hundred and sixty two Five hundred and sixty three Five hundred and sixty four Five hundred and sixty five Five hundred and sixty six Five hundred and sixty seven Five hundred and sixty eight Five hundred and sixty nine Five hundred and seventy Five hundred and seventy one Five hundred and seventy two Five hundred and seventy three Five hundred and seventy four Five hundred and seventy five Five hundred and seventy six Five hundred and seventy seven Five hundred and seventy eight Five hundred and seventy nine Five hundred and eighty Five hundred and eighty one Five hundred and eighty two Five hundred and eighty three Five hundred and eighty four Five hundred and eighty five Five hundred and eighty six Five hundred and eighty seven Five hundred and eighty eight Five hundred and eighty nine Five hundred and ninety Five hundred and ninety one Five hundred and ninety two Five hundred and ninety three Five hundred and ninety four Five hundred and ninety five Five hundred and ninety six Five hundred and ninety seven Five hundred and ninety eight Five hundred and ninety nine Six hundred Six hundred and one Six hundred and two Six hundred and three Six hundred and four Six hundred and five Six hundred and six Six hundred and seven Six hundred and eight Six hundred and nine Six hundred and ten Six hundred and eleven Six hundred and twelve Six hundred and thirteen Six hundred and fourteen Six hundred and fifteen Six hundred and sixteen Six hundred and seventeen Six hundred and eighteen Six hundred and nineteen Six hundred and twenty Six hundred and twenty one Six hundred and twenty two Six hundred and twenty three Six hundred and twenty four Six hundred and twenty five Six hundred and twenty six Six hundred and twenty seven Six hundred and twenty eight Six hundred and twenty nine Six hundred and thirty Six hundred and thirty one Six hundred and thirty two Six hundred and thirty three Six hundred and thirty four Six hundred and thirty five Six hundred and thirty six Six hundred and thirty seven Six hundred and thirty eight Six hundred and thirty nine Six hundred and forty Six hundred and forty one Six hundred and forty two Six hundred and forty three Six hundred and forty four Six hundred and forty five Six hundred and forty six Six hundred and forty seven Six hundred and forty eight Six hundred and forty nine Six hundred and fifty Six hundred and fifty one Six hundred and fifty two Six hundred and fifty three Six hundred and fifty four Six hundred and fifty five Six hundred and fifty six Six hundred and fifty seven Six hundred and fifty eight Six hundred and fifty nine Six hundred and sixty Six hundred and sixty one Six hundred and sixty two Six hundred and sixty three Six hundred and sixty four Six hundred and sixty five Six hundred and sixty six Six hundred and sixty seven Six hundred and sixty eight Six hundred and sixty nine Six hundred and seventy Six hundred and seventy one Six hundred and seventy two Six hundred and seventy three Six hundred and seventy four Six hundred and seventy five Six hundred and seventy six Six hundred and seventy seven Six hundred and seventy eight Six hundred and seventy nine Six hundred and eighty Six hundred and eighty one Six hundred and eighty two Six hundred and eighty three Six hundred and eighty four Six hundred and eighty five Six hundred and eighty six Six hundred and eighty seven Six hundred and eighty eight Six hundred and eighty nine Six hundred and ninety Six hundred and ninety one Six hundred and ninety two Six hundred and ninety three Six hundred and ninety four Six hundred and ninety five Six hundred and ninety six Six hundred and ninety seven Six hundred and ninety eight Six hundred and ninety nine Seven hundred Seven hundred and one Seven hundred and two Seven hundred and three Seven hundred and four Seven hundred and five Seven hundred and six Seven hundred and seven Seven hundred and eight Seven hundred and nine Seven hundred and ten Seven hundred and eleven Seven hundred and twelve Seven hundred and thirteen Seven hundred and fourteen Seven hundred and fifteen Seven hundred and sixteen Seven hundred and seventeen Seven hundred and eighteen Seven hundred and nineteen Seven hundred and twenty Seven hundred and twenty one Seven hundred and twenty two Seven hundred and twenty three Seven hundred and twenty four Seven hundred and twenty five Seven hundred and twenty six Seven hundred and twenty seven Seven hundred and twenty eight Seven hundred and twenty nine Seven hundred and thirty Seven hundred and thirty one Seven hundred and thirty two Seven hundred and thirty three Seven hundred and thirty four Seven hundred and thirty five Seven hundred and thirty six Seven hundred and thirty seven Seven hundred and thirty eight Seven hundred and thirty nine Seven hundred and forty Seven hundred and forty one Seven hundred and forty two Seven hundred and forty three Seven hundred and forty four Seven hundred and forty five Seven hundred and forty six Seven hundred and forty seven Seven hundred and forty eight Seven hundred and forty nine Seven hundred and fifty Seven hundred and fifty one Seven hundred and fifty two Seven hundred and fifty three Seven hundred and fifty four Seven hundred and fifty five Seven hundred and fifty six Seven hundred and fifty seven Seven hundred and fifty eight Seven hundred and fifty nine Seven hundred and sixty Seven hundred and sixty one Seven hundred and sixty two Seven hundred and sixty three Seven hundred and sixty four Seven hundred and sixty five Seven hundred and sixty six Seven hundred and sixty seven Seven hundred and sixty eight Seven hundred and sixty nine Seven hundred and seventy Seven hundred and seventy one Seven hundred and seventy two Seven hundred and seventy three Seven hundred and seventy four Seven hundred and seventy five Seven hundred and seventy six Seven hundred and seventy seven Seven hundred and seventy eight Seven hundred and seventy nine Seven hundred and eighty Seven hundred and eighty one Seven hundred and eighty two Seven hundred and eighty three Seven hundred and eighty four Seven hundred and eighty five Seven hundred and eighty six Seven hundred and eighty seven Seven hundred and eighty eight Seven hundred and eighty nine Seven hundred and ninety Seven hundred and ninety one Seven hundred and ninety two Seven hundred and ninety three Seven hundred and ninety four Seven hundred and ninety five Seven hundred and ninety six Seven hundred and ninety seven Seven hundred and ninety eight Seven hundred and ninety nine Eight hundred Eight hundred and one Eight hundred and two Eight hundred and three Eight hundred and four Eight hundred and five Eight hundred and six Eight hundred and seven Eight hundred and eight Eight hundred and nine Eight hundred and ten Eight hundred and eleven Eight hundred and twelve Eight hundred and thirteen Eight hundred and fourteen Eight hundred and fifteen Eight hundred and sixteen Eight hundred and seventeen Eight hundred and eighteen Eight hundred and nineteen Eight hundred and twenty Eight hundred and twenty one Eight hundred and twenty two Eight hundred and twenty three Eight hundred and twenty four Eight hundred and twenty five Eight hundred and twenty six Eight hundred and twenty seven Eight hundred and twenty eight Eight hundred and twenty nine Eight hundred and thirty Eight hundred and thirty one Eight hundred and thirty two Eight hundred and thirty three Eight hundred and thirty four Eight hundred and thirty five Eight hundred and thirty six Eight hundred and thirty seven Eight hundred and thirty eight Eight hundred and thirty nine Eight hundred and forty Eight hundred and forty one Eight hundred and forty two Eight hundred and forty three Eight hundred and forty four Eight hundred and forty five Eight hundred and forty six Eight hundred and forty seven Eight hundred and forty eight Eight hundred and forty nine Eight hundred and fifty Eight hundred and fifty one Eight hundred and fifty two Eight hundred and fifty three Eight hundred and fifty four Eight hundred and fifty five Eight hundred and fifty six Eight hundred and fifty seven Eight hundred and fifty eight Eight hundred and fifty nine Eight hundred and sixty Eight hundred and sixty one Eight hundred and sixty two Eight hundred and sixty three Eight hundred and sixty four Eight hundred and sixty five Eight hundred and sixty six Eight hundred and sixty seven Eight hundred and sixty eight Eight hundred and sixty nine Eight hundred and seventy Eight hundred and seventy one Eight hundred and seventy two Eight hundred and seventy three Eight hundred and seventy four Eight hundred and seventy five Eight hundred and seventy six Eight hundred and seventy seven Eight hundred and seventy eight Eight hundred and seventy nine Eight hundred and eighty Eight hundred and eighty one Eight hundred and eighty two Eight hundred and eighty three Eight hundred and eighty four Eight hundred and eighty five Eight hundred and eighty six Eight hundred and eighty seven Eight hundred and eighty eight Eight hundred and eighty nine Eight hundred and ninety Eight hundred and ninety one Eight hundred and ninety two Eight hundred and ninety three Eight hundred and ninety four Eight hundred and ninety five Eight hundred and ninety six Eight hundred and ninety seven Eight hundred and ninety eight Eight hundred and ninety nine Nine hundred Nine hundred and one Nine hundred and two Nine hundred and three Nine hundred and four Nine hundred and five Nine hundred and six Nine hundred and seven Nine hundred and eight Nine hundred and nine Nine hundred and ten Nine hundred and eleven Nine hundred and twelve Nine hundred and thirteen Nine hundred and fourteen Nine hundred and fifteen Nine hundred and sixteen Nine hundred and seventeen Nine hundred and eighteen Nine hundred and nineteen Nine hundred and twenty Nine hundred and twenty one Nine hundred and twenty two Nine hundred and twenty three Nine hundred and twenty four Nine hundred and twenty five Nine hundred and twenty six Nine hundred and twenty seven Nine hundred and twenty eight Nine hundred and twenty nine Nine hundred and thirty Nine hundred and thirty one Nine hundred and thirty two Nine hundred and thirty three Nine hundred and thirty four Nine hundred and thirty five Nine hundred and thirty six Nine hundred and thirty seven Nine hundred and thirty eight Nine hundred and thirty nine Nine hundred and forty Nine hundred and forty one Nine hundred and forty two Nine hundred and forty three Nine hundred and forty four Nine hundred and forty five Nine hundred and forty six Nine hundred and forty seven Nine hundred and forty eight Nine hundred and forty nine Nine hundred and fifty Nine hundred and fifty one Nine hundred and fifty two Nine hundred and fifty three Nine hundred and fifty four Nine hundred and fifty five Nine hundred and fifty six Nine hundred and fifty seven Nine hundred and fifty eight Nine hundred and fifty nine Nine hundred and sixty Nine hundred and sixty one Nine hundred and sixty two Nine hundred and sixty three Nine hundred and sixty four Nine hundred and sixty five Nine hundred and sixty six Nine hundred and sixty seven Nine hundred and sixty eight Nine hundred and sixty nine Nine hundred and seventy Nine hundred and seventy one Nine hundred and seventy two Nine hundred and seventy three Nine hundred and seventy four Nine hundred and seventy five Nine hundred and seventy six Nine hundred and seventy seven Nine hundred and seventy eight Nine hundred and seventy nine Nine hundred and eighty Nine hundred and eighty one Nine hundred and eighty two Nine hundred and eighty three Nine hundred and eighty four Nine hundred and eighty five Nine hundred and eighty six Nine hundred and eighty seven Nine hundred and eighty eight Nine hundred and eighty nine Nine hundred and ninety Nine hundred and ninety one Nine hundred and ninety two Nine hundred and ninety three Nine hundred and ninety four Nine hundred and ninety five Nine hundred and ninety six Nine hundred and ninety seven Nine hundred and ninety eight Nine hundred and ninety nine One thousand"
================================================
FILE: project_euler/problem_17/problem17.go
================================================
/**
* Problem 17 - Number letter counts
* @see {@link https://projecteuler.net/problem=17}
*
* If the numbers 1 to 5 are written out in words: one, two, three, four, five,
* then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
*
* If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words,
* how many letters would be used?
*
* NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two)
* contains 23 letters and 115 (one hundred and fifteen) contains 20 letters.
* The use of "and" when writing out numbers is in compliance with British usage.
*
* @author ddaniel27
*/
package problem17
import "strings"
func Problem17(input string) int {
var sum int
parsed := strings.Split(input, " ")
for _, word := range parsed {
sum += len(word)
}
return sum
}
================================================
FILE: project_euler/problem_17/problem17_test.go
================================================
package problem17
import "testing"
// Tests
func TestProblem17_Func(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{"1 to 5", "one two three four five", 19},
{"1 to 1000", INPUT, 21124},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Problem17(tt.input); got != tt.want {
t.Errorf("Problem17() = %v, want %v", got, tt.want)
}
})
}
}
// Benchmark
func BenchmarkProblem17_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem17(INPUT)
}
}
================================================
FILE: project_euler/problem_18/edge.go
================================================
package problem18
type Edge struct {
ID int
NodeValue NodeValue
NodeLeft Node
NodeRight Node
Parent Node
}
func (n *Edge) Value() NodeValue {
return n.NodeValue
}
func (n *Edge) Left() Node {
return n.NodeLeft
}
func (n *Edge) Right() Node {
return n.NodeRight
}
func (n *Edge) Kind() string {
return "edge"
}
func (n *Edge) CreateChild(value NodeValue, id int) Node {
// When the left child is nil, it's a left edge
if n.NodeLeft == nil {
return &Edge{
ID: id,
NodeValue: value,
Parent: n,
NodeLeft: nil,
NodeRight: nil,
}
}
// When the left child is a leaf, it's a right edge
if n.NodeLeft.Kind() == "leaf" {
return &Edge{
ID: id,
NodeValue: value,
Parent: n,
NodeLeft: nil,
NodeRight: nil,
}
}
return &Leaf{
ID: id,
NodeValue: value,
Parent: n,
NodeLeft: nil,
NodeRight: nil,
}
}
func (n *Edge) GetID() int {
return n.ID
}
func (n *Edge) Insert(node Node) {
// If Left is nil, always simply insert the node
if n.NodeLeft == nil {
node.SetParent(n)
n.NodeLeft = node
return
}
// If Right is nil, insert the node
n.NodeRight = node
// If the node to insert is an edge, set the parent
if node.Kind() == "edge" {
node.SetParent(n)
return
}
// If the node to insert is a leaf, send it to the sibling right
n.Parent.Right().Insert(node)
}
func (n *Edge) HasSpace() bool {
return n.NodeLeft == nil || n.NodeRight == nil
}
func (n *Edge) LeftIsNil() bool {
return n.NodeLeft == nil
}
func (n *Edge) RightIsNil() bool {
return n.NodeRight == nil
}
func (n *Edge) SetParent(node Node) {
n.Parent = node
}
================================================
FILE: project_euler/problem_18/input.go
================================================
package problem18
import "strings"
const problem18_input_string = `
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
`
var problem18_input_parsed_string []string = strings.Split(
strings.Trim(
strings.ReplaceAll(problem18_input_string, "\n", " "),
" ",
),
" ")
const problem18_test_string = `
3
7 4
2 4 6
8 5 9 3
`
var problem18_test_parsed_string []string = strings.Split(
strings.Trim(
strings.ReplaceAll(problem18_test_string, "\n", " "),
" ",
),
" ")
================================================
FILE: project_euler/problem_18/leaf.go
================================================
package problem18
type Leaf struct {
ID int
NodeValue NodeValue
NodeLeft *Leaf
NodeRight *Leaf
Parent Node
}
func (n *Leaf) Value() NodeValue {
return n.NodeValue
}
func (n *Leaf) Left() Node {
if n.NodeLeft != nil {
n.NodeLeft.Parent = n // Leaf is the parent of its left child always
}
return n.NodeLeft
}
func (n *Leaf) Right() Node {
return n.NodeRight
}
func (n *Leaf) Kind() string {
return "leaf"
}
func (n *Leaf) CreateChild(value NodeValue, id int) Node {
// Leafs only have leaf children
return &Leaf{
ID: id,
NodeValue: value,
Parent: n,
NodeLeft: nil,
NodeRight: nil,
}
}
func (n *Leaf) GetID() int {
return n.ID
}
func (n *Leaf) Insert(node Node) {
// If Left is nil, always simply insert the node
if n.NodeLeft == nil {
node.SetParent(n)
n.NodeLeft = node.(*Leaf)
return
}
// If Right is nil, insert the node
n.NodeRight = node.(*Leaf)
// Send it to the sibling right
n.Parent.Right().Insert(node)
}
func (n *Leaf) HasSpace() bool {
return n.NodeLeft == nil || n.NodeRight == nil
}
func (n *Leaf) LeftIsNil() bool {
return n.NodeLeft == nil
}
func (n *Leaf) RightIsNil() bool {
return n.NodeRight == nil
}
func (n *Leaf) SetParent(node Node) {
n.Parent = node
}
================================================
FILE: project_euler/problem_18/problem18.go
================================================
/**
* Problem 18 - Maximum path sum I
* @see {@link https://projecteuler.net/problem=18}
*
* By starting at the top of the triangle below and
* moving to adjacent numbers on the row below,
* the maximum total from top to bottom is 23.
*
* 3
* 7 4
* 2 4 6
* 8 5 9 3
*
* That is, 3 + 7 + 4 + 9 = 23.
*
* Find the maximum total from top to bottom of the triangle below:
* [refer to the problem link]
*
* NOTE: As there are only 16384 routes, it is possible
* to solve this problem by trying every route.
* However, Problem 67, is the same challenge with a triangle
* containing one-hundred rows; it cannot be solved by brute force,
* and requires a clever method! ;o)
*
* @author ddaniel27
*/
package problem18
import "strconv"
type (
NodeValue int
NodeType string
Node interface {
Value() NodeValue
GetID() int
Left() Node
Right() Node
LeftIsNil() bool
RightIsNil() bool
HasSpace() bool
Kind() string
SetParent(Node)
CreateChild(NodeValue, int) Node
Insert(Node)
}
)
func Problem18(input []string, deep int) int {
tree := &Tree{}
for _, num := range input {
v, err := strconv.Atoi(num)
if err != nil {
panic(err)
}
tree.BFSInsert(NodeValue(v))
}
return tree.MaxPathValueSearch(deep)
}
================================================
FILE: project_euler/problem_18/problem18_test.go
================================================
package problem18
import "testing"
// Tests
func TestProblem18_Func(t *testing.T) {
tests := []struct {
name string
input []string
deep int
expected int
}{
{
name: "Test case 1",
input: problem18_test_parsed_string,
deep: 2,
expected: 23,
},
{
name: "Test case 2",
input: problem18_input_parsed_string,
deep: 2,
expected: 1074,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := Problem18(test.input, test.deep)
if actual != test.expected {
t.Errorf("Expected %d, but got %d", test.expected, actual)
}
})
}
}
// Benchmarks
func BenchmarkProblem18_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem18(problem18_input_parsed_string, 2)
}
}
================================================
FILE: project_euler/problem_18/root.go
================================================
package problem18
type Root struct {
ID int
NodeValue NodeValue
NodeLeft *Edge
NodeRight *Edge
}
func (n *Root) Value() NodeValue {
return n.NodeValue
}
func (n *Root) Left() Node {
return n.NodeLeft
}
func (n *Root) Right() Node {
return n.NodeRight
}
func (n *Root) Kind() string {
return "root"
}
func (n *Root) CreateChild(value NodeValue, id int) Node {
return &Edge{
ID: id,
NodeValue: value,
Parent: n,
NodeLeft: nil,
NodeRight: nil,
}
}
func (n *Root) GetID() int {
return n.ID
}
func (n *Root) Insert(node Node) {
if n.NodeLeft == nil {
n.NodeLeft = node.(*Edge)
} else {
n.NodeRight = node.(*Edge)
}
}
func (n *Root) HasSpace() bool {
return n.NodeLeft == nil || n.NodeRight == nil
}
func (n *Root) LeftIsNil() bool {
return n.NodeLeft == nil
}
func (n *Root) RightIsNil() bool {
return n.NodeRight == nil
}
func (n *Root) SetParent(node Node) {
panic("Root node cannot have a parent")
}
================================================
FILE: project_euler/problem_18/tree.go
================================================
package problem18
import (
"fmt"
"strings"
)
type Tree struct {
Root *Root
Nodes map[int]struct{}
ID int
}
func (t *Tree) BFSInsert(value NodeValue) {
t.Nodes = make(map[int]struct{}) // Reset the nodes map
if t.Root == nil {
t.Root = &Root{NodeValue: value, ID: 0}
t.ID = 1
return
}
queue := make([]Node, 0)
queue = append(queue, t.Root)
t.isInQueue(t.Root.GetID())
head := 0
for head < len(queue) {
current := queue[head]
head++
childNode := current.CreateChild(value, t.ID)
if current.HasSpace() {
current.Insert(childNode)
t.ID++
return
}
if !t.isInQueue(current.Left().GetID()) { // Avoid duplicates
queue = append(queue, current.Left())
}
if !t.isInQueue(current.Right().GetID()) {
queue = append(queue, current.Right())
}
}
}
// MaxPathValueSearch is a method that searches the maximum path value in a tree
// given a certain depth
func (r *Tree) MaxPathValueSearch(deep int) int {
if r.Root == nil {
return 0
}
type DFSNode struct {
Node Node
StepsLeft int
Sum int
}
var pivot Node = r.Root
pivotEnded := false
maxPathValue := int(pivot.Value())
stack := make([]DFSNode, 0)
for !pivotEnded {
stack = append(stack, DFSNode{Node: pivot, StepsLeft: deep, Sum: maxPathValue})
for len(stack) > 0 {
current := stack[len(stack)-1]
stack = stack[:len(stack)-1]
// If we run out of steps, we check the sum of the path,
// update the maxPathValue if necessary and continue
if current.StepsLeft == 0 {
if current.Sum > maxPathValue {
maxPathValue = current.Sum
pivot = current.Node
}
continue
}
if !current.Node.RightIsNil() {
stack = append(stack, DFSNode{
Node: current.Node.Right(),
StepsLeft: current.StepsLeft - 1,
Sum: current.Sum + int(current.Node.Right().Value()),
})
}
// If the left child is nil, we have reached the end of the path
if !current.Node.LeftIsNil() {
stack = append(stack, DFSNode{
Node: current.Node.Left(),
StepsLeft: current.StepsLeft - 1,
Sum: current.Sum + int(current.Node.Left().Value()),
})
} else {
if current.Sum > maxPathValue {
maxPathValue = current.Sum
pivot = current.Node
}
}
}
// If we don't have reached the end of the left side of the tree,
// we continue with the search using the pivot node
// We use the left child only because how the tree is built
if pivot.LeftIsNil() {
pivotEnded = true
}
}
return maxPathValue
}
// PrintReport is a method that prints a report of the tree
// Node by node
func (t *Tree) PrintReport() {
t.Nodes = make(map[int]struct{})
if t.Root == nil {
return
}
queue := make([]Node, 0)
queue = append(queue, t.Root)
head := 0
for head < len(queue) {
current := queue[head]
head++
print("ID:", current.GetID())
print(", Current node:", current.Value())
if !current.LeftIsNil() {
print(", Left Child:", current.Left().Value())
if !t.isInQueue(current.Left().GetID()) {
queue = append(queue, current.Left())
}
}
if !current.RightIsNil() {
print(", Right Child:", current.Right().Value())
if !t.isInQueue(current.Right().GetID()) {
queue = append(queue, current.Right())
}
}
println()
}
}
// PrintPyramid is a method that prints the tree as a pyramid
func (t *Tree) PrintPyramid() {
t.Nodes = make(map[int]struct{})
if t.Root == nil {
return
}
queue := []Node{t.Root}
levels := []int{0}
outputByLevel := []string{}
head := 0
currentLevel := 0
output := ""
for head < len(queue) {
current := queue[head]
level := levels[head]
head++
// Level change
if level > currentLevel {
currentLevel = level
outputByLevel = append(outputByLevel, output+"\n")
output = ""
}
// Add current node to the output
if current.Value() < 10 {
output += fmt.Sprintf("0%d ", current.Value())
} else {
output += fmt.Sprintf("%d ", current.Value())
}
// Add children to the queue
if !current.LeftIsNil() {
if !t.isInQueue(current.Left().GetID()) {
queue = append(queue, current.Left())
levels = append(levels, level+1)
}
}
if !current.RightIsNil() {
if !t.isInQueue(current.Right().GetID()) {
queue = append(queue, current.Right())
levels = append(levels, level+1)
}
}
}
// Add the last level
outputByLevel = append(outputByLevel, output+"\n")
totalLevels := len(outputByLevel)
// Print the pyramid
for i, level := range outputByLevel {
str := strings.Repeat(" ", 2*(totalLevels-i)) + level
print(str)
}
}
// isInQueue is a method that avoids duplicates in the tree
func (n *Tree) isInQueue(nv int) bool {
if _, ok := n.Nodes[nv]; ok {
return true
}
n.Nodes[nv] = struct{}{}
return false
}
================================================
FILE: project_euler/problem_19/problem19.go
================================================
package problem19
/**
* Problem 19 - Counting Sundays
* @see {@link https://projecteuler.net/problem=19}
*
* You are given the following information,
* but you may prefer to do some research for yourself.
*
* 1 Jan 1900 was a Monday.
* Thirty days has September,
* April, June and November.
* All the rest have thirty-one,
* Saving February alone,
* Which has twenty-eight, rain or shine.
* And on leap years, twenty-nine.
* A leap year occurs on any year evenly divisible by 4,
* but not on a century unless it is divisible by 400.
*
* How many Sundays fell on the first of the month during
* the twentieth century (1 Jan 1901 to 31 Dec 2000)?
*
* @author ddaniel27
*/
func Problem19() int {
count := 0
dayOfWeek := 2 // 1 Jan 1901 was a Tuesday
for year := 1901; year <= 2000; year++ {
for month := 1; month <= 12; month++ {
if dayOfWeek == 0 {
count++
}
daysInMonth := 31
switch month {
case 4, 6, 9, 11:
daysInMonth = 30
case 2:
if IsLeapYear(year) {
daysInMonth = 29
} else {
daysInMonth = 28
}
}
dayOfWeek = (dayOfWeek + daysInMonth) % 7
}
}
return count
}
func IsLeapYear(year int) bool {
if year%4 == 0 {
if year%100 == 0 {
return year%400 == 0
}
return true
}
return false
}
================================================
FILE: project_euler/problem_19/problem19_test.go
================================================
package problem19
import "testing"
// Tests
func TestProblem19_Func(t *testing.T) {
tests := []struct {
name string
expected int
}{
{"Problem 19 - Counting Sundays", 171},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Problem19()
if got != test.expected {
t.Errorf("Problem19() = got %v, want %v", got, test.expected)
}
})
}
}
// Benchmarks
func BenchmarkProblem19_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem19()
}
}
================================================
FILE: project_euler/problem_2/problem2.go
================================================
/**
* Problem 2 - Even Fibonacci numbers
* @see {@link https://projecteuler.net/problem=2}
*
* Each new term in the Fibonacci sequence is generated by adding the previous two terms.
* By starting with 1 and 2, the first 10 terms will be:
*
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* By considering the terms in the Fibonacci sequence whose values do not exceed four million,
* find the sum of the even-valued terms.
*
* @author ddaniel27
*/
package problem2
func Problem2(n uint) uint {
sum := uint(0)
a, b := uint(1), uint(2)
for b < n {
if b%2 == 0 {
sum += b
}
a, b = b, a+b
}
return sum
}
================================================
FILE: project_euler/problem_2/problem2_test.go
================================================
package problem2
import "testing"
// Tests
func TestProblem2_Func(t *testing.T) {
tests := []struct {
name string
input uint
want uint
}{
{
name: "Testcase 1 - input 10",
input: 10,
want: 10,
},
{
name: "Testcase 2 - input 100",
input: 100,
want: 44,
},
{
name: "Testcase 3 - input 4e6",
input: 4e6,
want: 4613732,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem2(tt.input)
if n != tt.want {
t.Errorf("Problem2() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem2(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem2(4e6)
}
}
================================================
FILE: project_euler/problem_20/problem20.go
================================================
/**
* Problem 20 - Factorial digit sum
* @see {@link https://projecteuler.net/problem=20}
*
* n! means n × (n − 1) × ... × 3 × 2 × 1
*
* For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
* and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
*
* Find the sum of the digits in the number 100!
*
* @author ddaniel27
*/
package problem20
import "math/big"
func Problem20(input int) int {
factorial := bigFactorial(input)
sum := 0
for _, digit := range factorial.String() {
sum += int(digit - '0')
}
return sum
}
// bigFactorial returns the factorial of n as a big.Int
// Use big package to handle large numbers
func bigFactorial(n int) *big.Int {
if n < 0 {
return big.NewInt(0)
}
if n == 0 {
return big.NewInt(1)
}
return big.NewInt(0).Mul(big.NewInt(int64(n)), bigFactorial(n-1))
}
================================================
FILE: project_euler/problem_20/problem20_test.go
================================================
package problem20
import "testing"
// Tests
func TestProblem20_Func(t *testing.T) {
tests := []struct {
name string
input int
expected int
}{
{"Problem 20 - Factorial digit sum", 10, 27},
{"Problem 20 - Factorial digit sum", 100, 648},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Problem20(test.input)
if got != test.expected {
t.Errorf("Problem20() = got %v, want %v", got, test.expected)
}
})
}
}
// Benchmarks
func BenchmarkProblem20_Func(b *testing.B) {
for i := 0; i < b.N; i++ {
Problem20(100)
}
}
================================================
FILE: project_euler/problem_3/problem3.go
================================================
/**
* Problem 3 - Largest prime factor
* @see {@link https://projecteuler.net/problem=3}
*
* The prime factors of 13195 are 5, 7, 13 and 29.
* What is the largest prime factor of the number 600851475143 ?
*
* @author ddaniel27
*/
package problem3
func Problem3(n uint) uint {
i := uint(2)
for n > 1 {
if n%i == 0 {
n /= i
} else {
i++
}
}
return i
}
================================================
FILE: project_euler/problem_3/problem3_test.go
================================================
package problem3
import "testing"
// Tests
func TestProblem3_Func(t *testing.T) {
tests := []struct {
name string
input uint
want uint
}{
{
name: "Testcase 1 - input 13195",
input: 13195,
want: 29,
},
{
name: "Testcase 2 - input 600851475143",
input: 600851475143,
want: 6857,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem3(tt.input)
if n != tt.want {
t.Errorf("Problem3() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem3(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem3(600851475143)
}
}
================================================
FILE: project_euler/problem_4/problem4.go
================================================
/**
* Problem 4 - Largest palindrome product
* @see {@link https://projecteuler.net/problem=4}
*
* A palindromic number reads the same both ways.
* The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
* Find the largest palindrome made from the product of two 3-digit numbers.
*
* @author ddaniel27
*/
package problem4
import (
"fmt"
"github.com/TheAlgorithms/Go/strings/palindrome"
)
func Problem4() uint {
max := uint(0)
for i := 999; i >= 100; i-- {
for j := 999; j >= 100; j-- {
n := uint(i * j)
if palindrome.IsPalindrome(fmt.Sprintf("%d", n)) && n > max {
max = n
}
}
}
return max
}
================================================
FILE: project_euler/problem_4/problem4_test.go
================================================
package problem4
import "testing"
// Tests
func TestProblem4_Func(t *testing.T) {
tests := []struct {
name string
want uint
}{
{
name: "Testcase 1",
want: 906609,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem4()
if n != tt.want {
t.Errorf("Problem4() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem4(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem4()
}
}
================================================
FILE: project_euler/problem_5/problem5.go
================================================
/**
* Problem 5 - Smallest multiple
* @see {@link https://projecteuler.net/problem=5}
*
* 2520 is the smallest number that can be divided by
* each of the numbers from 1 to 10 without any remainder.
* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
*
* @author ddaniel27
*/
package problem5
func Problem5(limit uint) uint {
n := limit * limit
for {
if isDivisible(n, limit) {
return n
}
n++
}
}
func isDivisible(n, limit uint) bool {
for i := uint(1); i <= limit; i++ {
if n%i != 0 {
return false
}
}
return true
}
================================================
FILE: project_euler/problem_5/problem5_test.go
================================================
package problem5
import "testing"
// Tests
func TestProblem5_Func(t *testing.T) {
tests := []struct {
name string
input uint
want uint
}{
{
name: "Testcase 1 - input 10",
input: 10,
want: 2520,
},
{
name: "Testcase 2 - input 20",
input: 20,
want: 232792560,
},
{
name: "Testcase 3 - input 5",
input: 5,
want: 60,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem5(tt.input)
if n != tt.want {
t.Errorf("Problem5() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem5(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem5(20)
}
}
================================================
FILE: project_euler/problem_6/problem6.go
================================================
/**
* Problem 6 - Sum square difference
* @see {@link https://projecteuler.net/problem=6}
*
* The sum of the squares of the first ten natural numbers is,
* 1^2 + 2^2 + ... + 10^2 = 385
*
* The square of the sum of the first ten natural numbers is,
* (1 + 2 + ... + 10)^2 = 55^2 = 3025
*
* Hence the difference between the sum of the squares of the first ten natural numbers
* and the square of the sum is 3025 − 385 = 2640.
*
* Find the difference between the sum of the squares of the first one hundred natural numbers
* and the square of the sum.
*
* @author ddaniel27
*/
package problem6
func Problem6(n uint) uint {
sumOfSquares := uint(0)
squareOfSum := uint(0)
for i := uint(1); i <= n; i++ {
sumOfSquares += i * i
squareOfSum += i
}
squareOfSum *= squareOfSum
return squareOfSum - sumOfSquares
}
================================================
FILE: project_euler/problem_6/problem6_test.go
================================================
package problem6
import "testing"
// Tests
func TestProblem6_Func(t *testing.T) {
tests := []struct {
name string
input uint
want uint
}{
{
name: "Testcase 1 - input 10",
input: 10,
want: 2640,
},
{
name: "Testcase 2 - input 100",
input: 100,
want: 25164150,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem6(tt.input)
if n != tt.want {
t.Errorf("Problem6() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem6(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem6(100)
}
}
================================================
FILE: project_euler/problem_7/problem7.go
================================================
/**
* Problem 7 - 10001st prime
* @see {@link https://projecteuler.net/problem=7}
*
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
* we can see that the 6th prime is 13.
*
* What is the 10 001st prime number?
*
* @author ddaniel27
*/
package problem7
import "github.com/TheAlgorithms/Go/math/prime"
func Problem7(n uint) int64 {
count, i := uint(0), int64(1)
for count < n {
i++
if prime.OptimizedTrialDivision(i) {
count++
}
}
return i
}
================================================
FILE: project_euler/problem_7/problem7_test.go
================================================
package problem7
import "testing"
// Tests
func TestProblem7_Func(t *testing.T) {
tests := []struct {
name string
input uint
want int64
}{
{
name: "Testcase 1 - input 6",
input: 6,
want: 13,
},
{
name: "Testcase 2 - input 10001",
input: 10001,
want: 104743,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem7(tt.input)
if n != tt.want {
t.Errorf("Problem7() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem7(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem7(10001)
}
}
================================================
FILE: project_euler/problem_8/problem8.go
================================================
/**
* Problem 8 - Largest product in a series
* @see {@link https://projecteuler.net/problem=8}
*
* The four adjacent digits in the 1000-digit number that
* have the greatest product are 9 × 9 × 8 × 9 = 5832.
* Find the thirteen adjacent digits in the 1000-digit number
* that have the greatest product. What is the value of this product?
*
* @author ddaniel27
*/
package problem8
const number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
func Problem8(window int) uint {
max := uint(0)
for i := 0; i < len(number)-window; i++ {
product := uint(1)
for j := 0; j < window; j++ {
n := uint(number[i+j] - '0')
product *= n
}
if product > max {
max = product
}
}
return max
}
================================================
FILE: project_euler/problem_8/problem8_test.go
================================================
package problem8
import "testing"
// Tests
func TestProblem8_Func(t *testing.T) {
tests := []struct {
name string
input int
want uint
}{
{
name: "Testcase 1 - input 4",
input: 4,
want: 5832,
},
{
name: "Testcase 2 - input 13",
input: 13,
want: 23514624000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem8(tt.input)
if n != tt.want {
t.Errorf("Problem8() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem8(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem8(13)
}
}
================================================
FILE: project_euler/problem_9/problem9.go
================================================
/**
* Problem 9 - Special Pythagorean triplet
* @see {@link https://projecteuler.net/problem=9}
*
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
* a^2 + b^2 = c^2
*
* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
*
* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
* Find the product abc.
*
* @author ddaniel27
*/
package problem9
func Problem9() uint {
for a := uint(1); a < 1000; a++ {
for b := a + 1; b < 1000; b++ {
c := 1000 - a - b
if a*a+b*b == c*c {
return a * b * c
}
}
}
return 0
}
================================================
FILE: project_euler/problem_9/problem9_test.go
================================================
package problem9
import "testing"
// Tests
func TestProblem9_Func(t *testing.T) {
tests := []struct {
name string
want uint
}{
{
name: "Testcase 1",
want: 31875000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := Problem9()
if n != tt.want {
t.Errorf("Problem9() = %v, want %v", n, tt.want)
}
})
}
}
// Benchmarks
func BenchmarkProblem9(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Problem9()
}
}
================================================
FILE: search/binary.go
================================================
package search
// Binary search for target within a sorted array by repeatedly dividing the array in half and comparing the midpoint with the target.
// This function uses recursive call to itself.
// If a target is found, the index of the target is returned. Else the function return -1 and ErrNotFound.
func Binary(array []int, target int, lowIndex int, highIndex int) (int, error) {
if highIndex < lowIndex || len(array) == 0 {
return -1, ErrNotFound
}
mid := int(lowIndex + (highIndex-lowIndex)/2)
if array[mid] > target {
return Binary(array, target, lowIndex, mid-1)
} else if array[mid] < target {
return Binary(array, target, mid+1, highIndex)
} else {
return mid, nil
}
}
// BinaryIterative search for target within a sorted array by repeatedly dividing the array in half and comparing the midpoint with the target.
// Unlike Binary, this function uses iterative method and not recursive.
// If a target is found, the index of the target is returned. Else the function return -1 and ErrNotFound.
func BinaryIterative(array []int, target int) (int, error) {
startIndex := 0
endIndex := len(array) - 1
var mid int
for startIndex <= endIndex {
mid = int(startIndex + (endIndex-startIndex)/2)
if array[mid] > target {
endIndex = mid - 1
} else if array[mid] < target {
startIndex = mid + 1
} else {
return mid, nil
}
}
return -1, ErrNotFound
}
// LowerBound returns index to the first element in the range [0, len(array)-1] that is not less than (i.e. greater or equal to) target.
// return -1 and ErrNotFound if no such element is found.
func LowerBound(array []int, target int) (int, error) {
startIndex := 0
endIndex := len(array) - 1
var mid int
for startIndex <= endIndex {
mid = int(startIndex + (endIndex-startIndex)/2)
if array[mid] < target {
startIndex = mid + 1
} else {
endIndex = mid - 1
}
}
//when target greater than every element in array, startIndex will out of bounds
if startIndex >= len(array) {
return -1, ErrNotFound
}
return startIndex, nil
}
// UpperBound returns index to the first element in the range [lowIndex, len(array)-1] that is greater than target.
// return -1 and ErrNotFound if no such element is found.
func UpperBound(array []int, target int) (int, error) {
startIndex := 0
endIndex := len(array) - 1
var mid int
for startIndex <= endIndex {
mid = int(startIndex + (endIndex-startIndex)/2)
if array[mid] > target {
endIndex = mid - 1
} else {
startIndex = mid + 1
}
}
//when target greater or equal than every element in array, startIndex will out of bounds
if startIndex >= len(array) {
return -1, ErrNotFound
}
return startIndex, nil
}
================================================
FILE: search/binary_test.go
================================================
package search
import (
"errors"
"testing"
)
func TestBinary(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := Binary(test.data, test.key, 0, len(test.data)-1)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if !errors.Is(test.expectedError, actualError) {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func TestBinaryIterative(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := BinaryIterative(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if !errors.Is(test.expectedError, actualError) {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func TestLowerBound(t *testing.T) {
for _, test := range lowerBoundTests {
actualValue, actualError := LowerBound(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if !errors.Is(test.expectedError, actualError) {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func TestUpperBound(t *testing.T) {
for _, test := range upperBoundTests {
actualValue, actualError := UpperBound(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if !errors.Is(test.expectedError, actualError) {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func BenchmarkBinary(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // this is important because the generateBenchmarkTestCase() is expensive
for i := 0; i < b.N; i++ {
_, _ = Binary(testCase, i, 0, len(testCase)-1)
}
}
func BenchmarkBinaryIterative(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // this is important because the generateBenchmarkTestCase() is expensive
for i := 0; i < b.N; i++ {
_, _ = BinaryIterative(testCase, i)
}
}
func BenchmarkLowerBound(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // this is important because the generateBenchmarkTestCase() is expensive
for i := 0; i < b.N; i++ {
_, _ = LowerBound(testCase, i)
}
}
func BenchmarkUpperBound(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // this is important because the generateBenchmarkTestCase() is expensive
for i := 0; i < b.N; i++ {
_, _ = UpperBound(testCase, i)
}
}
================================================
FILE: search/doc.go
================================================
// Package search is a subpackage dedicated to all searching algorithms related to slices/arrays.
package search
================================================
FILE: search/errors.go
================================================
package search
import "errors"
// ErrNotFound is returned by search functions when target is not found
var ErrNotFound = errors.New("target not found in array")
================================================
FILE: search/interpolation.go
================================================
package search
// Interpolation searches for the entity in the given sortedData.
// if the entity is present, it will return the index of the entity, if not -1 will be returned.
// see: https://en.wikipedia.org/wiki/Interpolation_search
// Complexity
//
// Worst: O(N)
// Average: O(log(log(N)) if the elements are uniformly distributed
// Best: O(1)
//
// Example
//
// fmt.Println(InterpolationSearch([]int{1, 2, 9, 20, 31, 45, 63, 70, 100},100))
func Interpolation(sortedData []int, guess int) (int, error) {
if len(sortedData) == 0 {
return -1, ErrNotFound
}
var (
low, high = 0, len(sortedData) - 1
lowVal, highVal = sortedData[low], sortedData[high]
)
for lowVal != highVal && (lowVal <= guess) && (guess <= highVal) {
mid := low + int(float64(float64((guess-lowVal)*(high-low))/float64(highVal-lowVal)))
// if guess is found, array can also have duplicate values, so scan backwards and find the first index
if sortedData[mid] == guess {
for mid > 0 && sortedData[mid-1] == guess {
mid--
}
return mid, nil
}
// adjust our guess and continue
if sortedData[mid] > guess {
high, highVal = mid-1, sortedData[high]
} else {
low, lowVal = mid+1, sortedData[low]
}
}
if guess == lowVal {
return low, nil
}
return -1, ErrNotFound
}
================================================
FILE: search/interpolation_test.go
================================================
package search
import "testing"
func TestInterpolation(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := Interpolation(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if actualError != test.expectedError {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func BenchmarkInterpolation(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // exclude time taken to generate test case
for i := 0; i < b.N; i++ {
_, _ = Interpolation(testCase, i)
}
}
================================================
FILE: search/jump.go
================================================
// jump.go
// description: Implementation of jump search
// details:
// A search algorithm for ordered list that jump through the list to narrow down the range
// before performing a linear search
// reference: https://en.wikipedia.org/wiki/Jump_search
// see jump_test.go for a test implementation, test function TestJump
// time complexity: O(sqrt(n))
// space complexity: O(1)
package search
import "math"
// Jump search works by jumping multiple steps ahead in sorted list until it find an item larger than target,
// then create a sublist of item from the last searched item up to the current item and perform a linear search.
func Jump(array []int, target int) (int, error) {
n := len(array)
if n == 0 {
return -1, ErrNotFound
}
// the optimal value of step is square root of the length of list
step := int(math.Round(math.Sqrt(float64(n))))
prev := 0 // previous index
curr := step // current index
for array[curr-1] < target {
prev = curr
if prev >= len(array) {
return -1, ErrNotFound
}
curr += step
// prevent jumping over list range
if curr > n {
curr = n
}
}
// perform linear search from index prev to index curr
for array[prev] < target {
prev++
// if reach end of range, indicate target not found
if prev == curr {
return -1, ErrNotFound
}
}
if array[prev] == target {
return prev, nil
}
return -1, ErrNotFound
}
================================================
FILE: search/jump2.go
================================================
package search
import "math"
func Jump2(arr []int, target int) (int, error) {
step := int(math.Round(math.Sqrt(float64(len(arr)))))
rbound := len(arr)
for i := step; i < len(arr); i += step {
if arr[i] > target {
rbound = i
break
}
}
for i := rbound - step; i < rbound; i++ {
if arr[i] == target {
return i, nil
}
if arr[i] > target {
break
}
}
return -1, ErrNotFound
}
================================================
FILE: search/jump2_test.go
================================================
package search
import "testing"
func TestJump2(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := Jump2(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if actualError != test.expectedError {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func BenchmarkJump2(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // exclude time taken to generate test case
for i := 0; i < b.N; i++ {
_, _ = Jump2(testCase, i)
}
}
================================================
FILE: search/jump_test.go
================================================
package search
import "testing"
func TestJump(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := Jump(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if actualError != test.expectedError {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func BenchmarkJump(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // exclude time taken to generate test case
for i := 0; i < b.N; i++ {
_, _ = Jump(testCase, i)
}
}
================================================
FILE: search/linear.go
================================================
package search
// Linear Simple linear search algorithm that iterates over all elements of an array in the worst case scenario
func Linear(array []int, query int) (int, error) {
for i, item := range array {
if item == query {
return i, nil
}
}
return -1, ErrNotFound
}
================================================
FILE: search/linear_test.go
================================================
package search
import (
"testing"
)
func TestLinear(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := Linear(test.data, test.key)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if actualError != test.expectedError {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func BenchmarkLinear(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // exclude time taken to generate test case
for i := 0; i < b.N; i++ {
_, _ = Linear(testCase, i)
}
}
================================================
FILE: search/selectk.go
================================================
package search
func SelectK(array []int, k int) (int, error) {
if k > len(array) {
return -1, ErrNotFound
}
return selectK(array, 0, len(array), len(array)-k), nil
}
// search the element which index is idx
func selectK(array []int, l, r, idx int) int {
index := partition(array, l, r)
if index == idx {
return array[index]
}
if index < idx {
return selectK(array, index+1, r, idx)
}
return selectK(array, l, index, idx)
}
func partition(array []int, l, r int) int {
elem, j := array[l], l+1
for i := l + 1; i < r; i++ {
if array[i] <= elem {
array[i], array[j] = array[j], array[i]
j++
}
}
array[l], array[j-1] = array[j-1], array[l]
return j - 1
}
================================================
FILE: search/selectk_test.go
================================================
package search
import (
"testing"
)
func TestSelectK(t *testing.T) {
tests := []struct {
data []int
k int
expected int
err error
name string
}{
{[]int{1, 2, 3, 4, 5}, 1, 5, nil, "sorted data"},
{[]int{5, 4, 3, 2, 1}, 2, 4, nil, "reversed data"},
{[]int{3, 1, 2, 5, 4}, 3, 3, nil, "random data"},
{[]int{3, 2, 1, 5, 4}, 10, -1, ErrNotFound, " absent data"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
elem, err := SelectK(tc.data, tc.k)
if err != tc.err {
t.Errorf("name:%v SelectK() = _, %v, want err: %v", tc.name, err, tc.err)
}
if elem != tc.expected {
t.Errorf("name:%v SelectK() = %v,_ , want: %v", tc.name, elem, tc.expected)
}
})
}
}
func BenchmarkSelectK(b *testing.B) {
testCase := generateBenchmarkTestCase()
testCase = append(testCase, 1) // make sure len(testCase)/2+1 is valid
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = SelectK(testCase, len(testCase)/2)
}
}
================================================
FILE: search/ternary.go
================================================
package search
import (
"fmt"
"math"
)
// TernaryMax is a function to search for maximum value of a uni-modal function `f`
// in the interval [a, b]. a and b should be finit numbers
func TernaryMax(a, b, epsilon float64, f func(x float64) float64) (float64, error) {
if a == math.Inf(-1) || b == math.Inf(1) {
return -1, fmt.Errorf("interval boundaries should be finite numbers")
}
if math.Abs(a-b) <= epsilon {
return f((a + b) / 2), nil
}
left := (2*a + b) / 3
right := (a + 2*b) / 3
if f(left) < f(right) {
return TernaryMax(left, b, epsilon, f)
}
return TernaryMax(a, right, epsilon, f)
}
// TernaryMin is a function to search for minimum value of a uni-modal function `f`
// in the interval [a, b]. a and b should be finit numbers.
func TernaryMin(a, b, epsilon float64, f func(x float64) float64) (float64, error) {
if a == math.Inf(-1) || b == math.Inf(1) {
return -1, fmt.Errorf("interval boundaries should be finite numbers")
}
if math.Abs(a-b) <= epsilon {
return f((a + b) / 2), nil
}
left := (2*a + b) / 3
right := (a + 2*b) / 3
if f(left) > f(right) {
return TernaryMin(left, b, epsilon, f)
}
return TernaryMin(a, right, epsilon, f)
}
================================================
FILE: search/ternary_test.go
================================================
package search
import (
"math"
"testing"
)
const EPS = 1e-6
func equal(a, b float64) bool {
return math.Abs(a-b) <= EPS
}
func TestTernaryMax(t *testing.T) {
var tests = []struct {
f func(x float64) float64
a float64
b float64
expected float64
}{
{f: func(x float64) float64 { return -x * x }, a: 1, b: -1, expected: 0},
{f: func(x float64) float64 { return -2*x*x - x + 1 }, a: -1, b: 1, expected: 1.125},
}
for _, test := range tests {
result, err := TernaryMax(test.a, test.b, EPS, test.f)
if err != nil {
t.Errorf("error occurred: %v", err)
}
if !equal(result, test.expected) {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.expected, result)
}
}
}
func TestTernaryMin(t *testing.T) {
var tests = []struct {
f func(x float64) float64
a float64
b float64
expected float64
}{
{f: func(x float64) float64 { return x * x }, a: -1, b: 1, expected: 0},
{f: func(x float64) float64 { return 2*x*x + x + 1 }, a: -1, b: 1, expected: 0.875},
}
for _, test := range tests {
result, err := TernaryMin(test.a, test.b, EPS, test.f)
if err != nil {
t.Errorf("error occurred: %v", err)
}
if !equal(result, test.expected) {
t.Errorf("Wrong result! Expected:%v, returned:%v ", test.expected, result)
}
}
}
================================================
FILE: search/testcases.go
================================================
package search
type searchTest struct {
data []int
key int
expected int
expectedError error
name string
}
// Note that these are immutable therefore they are shared among all the search tests.
// If your algorithm is mutating these then it is advisable to create separate test cases.
var searchTests = []searchTest{
//Sanity
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10, 9, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 9, 8, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 8, 7, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 7, 6, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 6, 5, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 4, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 4, 3, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 3, 2, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2, 1, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 1, 0, nil, "Sanity"},
//Absent
{[]int{1, 4, 5, 6, 7, 10}, -25, -1, ErrNotFound, "Absent"},
{[]int{1, 4, 5, 6, 7, 10}, 25, -1, ErrNotFound, "Absent"},
//Empty slice
{[]int{}, 2, -1, ErrNotFound, "Empty"},
}
var lowerBoundTests = []searchTest{
//Sanity
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, -25, 0, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 1, 0, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 4, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10, 9, nil, "Sanity"},
{[]int{1, 2, 2, 2, 2, 6, 7, 8, 9, 10}, 2, 1, nil, "Sanity"},
{[]int{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 2, 0, nil, "Sanity"},
//Absent
{[]int{1, 4, 5, 6, 7, 10}, 25, -1, ErrNotFound, "Absent"},
//Empty slice
{[]int{}, 2, -1, ErrNotFound, "Empty"},
}
var upperBoundTests = []searchTest{
//Sanity
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, -25, 0, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 1, 1, nil, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 5, nil, "Sanity"},
{[]int{1, 2, 2, 2, 2, 6, 7, 8, 9, 10}, 2, 5, nil, "Sanity"},
//Absent
{[]int{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 2, -1, ErrNotFound, "Sanity"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10, -1, ErrNotFound, "Sanity"},
{[]int{1, 4, 5, 6, 7, 10}, 25, -1, ErrNotFound, "Absent"},
//Empty slice
{[]int{}, 2, -1, ErrNotFound, "Empty"},
}
// This function generate consistent testcase for benchmark test.
func generateBenchmarkTestCase() []int {
var testCase []int
for i := 0; i < 1000; i++ {
testCase = append(testCase, i)
}
return testCase
}
================================================
FILE: sort/binaryinsertionsort.go
================================================
// Binary Insertion Sort
// description: Implementation of binary insertion sort in Go
// details: Binary Insertion Sort is a variation of
// Insertion sort in which proper location to
// insert the selected element is found using the
// Binary search algorithm.
// ref: https://www.geeksforgeeks.org/binary-insertion-sort
package sort
import "github.com/TheAlgorithms/Go/constraints"
func BinaryInsertion[T constraints.Ordered](arr []T) []T {
for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
temporary := arr[currentIndex]
low := 0
high := currentIndex - 1
for low <= high {
mid := low + (high-low)/2
if arr[mid] > temporary {
high = mid - 1
} else {
low = mid + 1
}
}
for itr := currentIndex; itr > low; itr-- {
arr[itr] = arr[itr-1]
}
arr[low] = temporary
}
return arr
}
================================================
FILE: sort/bogosort.go
================================================
// This is a pure Go implementation of the bogosort algorithm,
// also known as permutation sort, stupid sort, slowsort, shotgun sort, or monkey sort.
// Bogosort generates random permutations until it guesses the correct one.
// worst-case time complexity: O((n+1)!)
// best-case time complexity: O(n)
// More info on: https://en.wikipedia.org/wiki/Bogosort
package sort
import (
"math/rand"
"github.com/TheAlgorithms/Go/constraints"
)
func isSorted[T constraints.Number](arr []T) bool {
for i := 0; i < len(arr)-1; i++ {
if arr[i] > arr[i+1] {
return false
}
}
return true
}
func shuffle[T constraints.Number](arr []T) {
for i := range arr {
j := rand.Intn(i + 1)
arr[i], arr[j] = arr[j], arr[i]
}
}
func Bogo[T constraints.Number](arr []T) []T {
for !isSorted(arr) {
shuffle(arr)
}
return arr
}
================================================
FILE: sort/bubblesort.go
================================================
// Implementation of basic bubble sort algorithm
// Reference: https://en.wikipedia.org/wiki/Bubble_sort
package sort
import "github.com/TheAlgorithms/Go/constraints"
// Bubble is a simple generic definition of Bubble sort algorithm.
func Bubble[T constraints.Ordered](arr []T) []T {
swapped := true
for swapped {
swapped = false
for i := 0; i < len(arr)-1; i++ {
if arr[i+1] < arr[i] {
arr[i+1], arr[i] = arr[i], arr[i+1]
swapped = true
}
}
}
return arr
}
================================================
FILE: sort/bucketsort.go
================================================
package sort
import "github.com/TheAlgorithms/Go/constraints"
// Bucket sorts a slice. It is mainly useful
// when input is uniformly distributed over a range.
func Bucket[T constraints.Number](arr []T) []T {
// early return if the array too small
if len(arr) <= 1 {
return arr
}
// find the maximum and minimum elements in arr
max := arr[0]
min := arr[0]
for _, v := range arr {
if v > max {
max = v
}
if v < min {
min = v
}
}
// create an empty bucket for each element in arr
bucket := make([][]T, len(arr))
// put each element in the appropriate bucket
for _, v := range arr {
bucketIndex := int((v - min) / (max - min) * T(len(arr)-1))
bucket[bucketIndex] = append(bucket[bucketIndex], v)
}
// use insertion sort to sort each bucket
for i := range bucket {
bucket[i] = Insertion(bucket[i])
}
// concatenate the sorted buckets
sorted := make([]T, 0, len(arr))
for _, v := range bucket {
sorted = append(sorted, v...)
}
return sorted
}
================================================
FILE: sort/circlesort.go
================================================
// Package sort implements various sorting algorithms.
package sort
import "github.com/TheAlgorithms/Go/constraints"
// Circle sorts an array using the circle sort algorithm.
func Circle[T constraints.Ordered](arr []T) []T {
if len(arr) == 0 {
return arr
}
for doSort(arr, 0, len(arr)-1) {
}
return arr
}
// doSort is the recursive function that implements the circle sort algorithm.
func doSort[T constraints.Ordered](arr []T, left, right int) bool {
if left == right {
return false
}
swapped := false
low := left
high := right
for low < high {
if arr[low] > arr[high] {
arr[low], arr[high] = arr[high], arr[low]
swapped = true
}
low++
high--
}
if low == high && arr[low] > arr[high+1] {
arr[low], arr[high+1] = arr[high+1], arr[low]
swapped = true
}
mid := left + (right-left)/2
leftHalf := doSort(arr, left, mid)
rightHalf := doSort(arr, mid+1, right)
return swapped || leftHalf || rightHalf
}
================================================
FILE: sort/cocktailsort.go
================================================
// Implementation of Cocktail sorting
// reference: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
package sort
import "github.com/TheAlgorithms/Go/constraints"
// Cocktail sort is a variation of bubble sort, operating in two directions (beginning to end, end to beginning)
func Cocktail[T constraints.Ordered](arr []T) []T {
if len(arr) == 0 { // ignore 0 length arrays
return arr
}
swapped := true // true if swapped two or more elements in the last loop
// if it loops through the array without swapping, the array is sorted
// start and end indexes, this will be updated excluding already sorted elements
start := 0
end := len(arr) - 1
for swapped {
swapped = false
var new_start int
var new_end int
for i := start; i < end; i++ { // first loop, from start to end
if arr[i] > arr[i+1] { // if current and next elements are unordered
arr[i], arr[i+1] = arr[i+1], arr[i] // swap two elements
new_end = i
swapped = true
}
}
end = new_end
if !swapped { // early exit, skipping the second loop
break
}
swapped = false
for i := end; i > start; i-- { // second loop, from end to start
if arr[i] < arr[i-1] { // same process of the first loop, now going 'backwards'
arr[i], arr[i-1] = arr[i-1], arr[i]
new_start = i
swapped = true
}
}
start = new_start
}
return arr
}
================================================
FILE: sort/combSort.go
================================================
// Implementation of comb sort algorithm, an improvement of bubble sort
// average time complexity: O(n^2 / 2^p) where p is the number of increments
// worst time complexity: O(n^2)
// space complexity: O(1)
// Reference: https://www.geeksforgeeks.org/comb-sort/
package sort
import "github.com/TheAlgorithms/Go/constraints"
func getNextGap(gap int) int {
gap = (gap * 10) / 13
if gap < 1 {
return 1
}
return gap
}
// Comb is a simple sorting algorithm which is an improvement of the bubble sorting algorithm.
func Comb[T constraints.Ordered](data []T) []T {
n := len(data)
gap := n
swapped := true
for gap != 1 || swapped {
gap = getNextGap(gap)
swapped = false
for i := 0; i < n-gap; i++ {
if data[i] > data[i+gap] {
data[i], data[i+gap] = data[i+gap], data[i]
swapped = true
}
}
}
return data
}
================================================
FILE: sort/countingsort.go
================================================
// countingsort.go
// description: Implementation of counting sort algorithm
// details: A simple counting sort algorithm implementation
// worst-case time complexity: O(n + k) where n is the number of elements in the input array and k is the range of the input
// average-case time complexity: O(n + k) where n is the number of elements in the input array and k is the range of the input
// space complexity: O(n + k)
// author [Phil](https://github.com/pschik)
// see sort_test.go for a test implementation, test function TestQuickSort
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Count[T constraints.Integer](data []T) []T {
if len(data) == 0 {
return data
}
var aMin, aMax = data[0], data[0]
for _, x := range data {
if x < aMin {
aMin = x
}
if x > aMax {
aMax = x
}
}
count := make([]int, aMax-aMin+1)
for _, x := range data {
count[x-aMin]++ // this is the reason for having only Integer constraint instead of Ordered.
}
z := 0
for i, c := range count {
for c > 0 {
data[z] = T(i) + aMin
z++
c--
}
}
return data
}
================================================
FILE: sort/cyclesort.go
================================================
package sort
import (
"github.com/TheAlgorithms/Go/constraints"
)
// Cycle sort is an in-place, unstable sorting algorithm that is particularly useful
// when sorting arrays containing elements with a small range of values. It is theoretically
// optimal in terms of the total number of writes to the original array.
func Cycle[T constraints.Number](arr []T) []T {
counter, cycle, len := 0, 0, len(arr)
// Early return if the array too small
if len <= 1 {
return arr
}
for cycle = 0; cycle < len-1; cycle++ {
elem := arr[cycle]
// Find total smaller elements to right
pos := cycle
for counter = cycle + 1; counter < len; counter++ {
if arr[counter] < elem {
pos++
}
}
// In case this element is already in correct position, let's skip processing
if pos == cycle {
continue
}
// In case we have same elements, we want to skip to the end of that list as well, ignoring order
// This makes the algorithm unstable for composite elements
for elem == arr[pos] {
pos++
}
// Now let us put the item to it's right position
arr[pos], elem = elem, arr[pos]
//We need to rotate the array till we have reached the start of the cycle again
for pos != cycle {
pos = cycle
// Find smaller elements to right again
for counter = cycle + 1; counter < len; counter++ {
if arr[counter] < elem {
pos++
}
}
for elem == arr[pos] {
pos++
}
//We can do this unconditionally, but the check helps prevent redundant writes to the array
if elem != arr[pos] {
arr[pos], elem = elem, arr[pos]
}
}
}
return arr
}
================================================
FILE: sort/doc.go
================================================
// Package sort a package for demonstrating sorting algorithms in Go
package sort
================================================
FILE: sort/exchangesort.go
================================================
// Implementation of exchange sort algorithm, a variant of bubble sort
// average time complexity: O(n^2)
// worst time complexity: O(n^2)
// space complexity: O(1)
// Reference: https://en.wikipedia.org/wiki/Sorting_algorithm#Exchange_sort
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Exchange[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr)-1; i++ {
for j := i + 1; j < len(arr); j++ {
if arr[i] > arr[j] {
arr[i], arr[j] = arr[j], arr[i]
}
}
}
return arr
}
================================================
FILE: sort/heapsort.go
================================================
// heapsort.go
// description: Implementation of heap sort algorithm
// worst-case time complexity: O(n log n)
// average-case time complexity: O(n log n)
// space complexity: O(1)
package sort
import "github.com/TheAlgorithms/Go/constraints"
type MaxHeap struct {
slice []Comparable
heapSize int
indices map[int]int
}
func (h *MaxHeap) Init(slice []Comparable) {
if slice == nil {
slice = make([]Comparable, 0)
}
h.slice = slice
h.heapSize = len(slice)
h.indices = make(map[int]int)
h.Heapify()
}
func (h MaxHeap) Heapify() {
for i, v := range h.slice {
h.indices[v.Idx()] = i
}
for i := h.heapSize / 2; i >= 0; i-- {
h.heapifyDown(i)
}
}
func (h *MaxHeap) Pop() Comparable {
if h.heapSize == 0 {
return nil
}
i := h.slice[0]
h.heapSize--
h.slice[0] = h.slice[h.heapSize]
h.updateidx(0)
h.heapifyDown(0)
h.slice = h.slice[0:h.heapSize]
return i
}
func (h *MaxHeap) Push(i Comparable) {
h.slice = append(h.slice, i)
h.updateidx(h.heapSize)
h.heapifyUp(h.heapSize)
h.heapSize++
}
func (h MaxHeap) Size() int {
return h.heapSize
}
func (h MaxHeap) Update(i Comparable) {
h.slice[h.indices[i.Idx()]] = i
h.heapifyUp(h.indices[i.Idx()])
h.heapifyDown(h.indices[i.Idx()])
}
func (h MaxHeap) updateidx(i int) {
h.indices[h.slice[i].Idx()] = i
}
func (h *MaxHeap) swap(i, j int) {
h.slice[i], h.slice[j] = h.slice[j], h.slice[i]
h.updateidx(i)
h.updateidx(j)
}
func (h MaxHeap) more(i, j int) bool {
return h.slice[i].More(h.slice[j])
}
func (h MaxHeap) heapifyUp(i int) {
if i == 0 {
return
}
p := i / 2
if h.slice[i].More(h.slice[p]) {
h.swap(i, p)
h.heapifyUp(p)
}
}
func (h MaxHeap) heapifyDown(i int) {
heapifyDown(h.slice, h.heapSize, i, h.more, h.swap)
}
func heapifyDown[T any](slice []T, N, i int, moreFunc func(i, j int) bool, swapFunc func(i, j int)) {
l, r := 2*i+1, 2*i+2
max := i
if l < N && moreFunc(l, max) {
max = l
}
if r < N && moreFunc(r, max) {
max = r
}
if max != i {
swapFunc(i, max)
heapifyDown(slice, N, max, moreFunc, swapFunc)
}
}
type Comparable interface {
Idx() int
More(any) bool
}
func HeapSort[T constraints.Ordered](slice []T) []T {
N := len(slice)
moreFunc := func(i, j int) bool {
return slice[i] > slice[j]
}
swapFunc := func(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
// build a maxheap
for i := N/2 - 1; i >= 0; i-- {
heapifyDown(slice, N, i, moreFunc, swapFunc)
}
for i := N - 1; i > 0; i-- {
slice[i], slice[0] = slice[0], slice[i]
heapifyDown(slice, i, 0, moreFunc, swapFunc)
}
return slice
}
================================================
FILE: sort/insertionsort.go
================================================
// insertionsort.go
// description: Implementation of insertion sort algorithm
// worst-case time complexity: O(n^2)
// average-case time complexity: O(n^2)
// space complexity: O(1)
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Insertion[T constraints.Ordered](arr []T) []T {
for currentIndex := 1; currentIndex < len(arr); currentIndex++ {
temporary := arr[currentIndex]
iterator := currentIndex
for ; iterator > 0 && arr[iterator-1] > temporary; iterator-- {
arr[iterator] = arr[iterator-1]
}
arr[iterator] = temporary
}
return arr
}
================================================
FILE: sort/mergesort.go
================================================
// mergesort.go
// description: Implementation of merge sort algorithm
// worst-case time complexity: O(n log n)
// average-case time complexity: O(n log n)
// space complexity: O(n)
package sort
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/min"
"sync"
)
func merge[T constraints.Ordered](a []T, b []T) []T {
var r = make([]T, len(a)+len(b))
var i = 0
var j = 0
for i < len(a) && j < len(b) {
if a[i] <= b[j] {
r[i+j] = a[i]
i++
} else {
r[i+j] = b[j]
j++
}
}
for i < len(a) {
r[i+j] = a[i]
i++
}
for j < len(b) {
r[i+j] = b[j]
j++
}
return r
}
// Merge Perform merge sort on a slice
func Merge[T constraints.Ordered](items []T) []T {
if len(items) < 2 {
return items
}
var middle = len(items) / 2
var a = Merge(items[:middle])
var b = Merge(items[middle:])
return merge(a, b)
}
func MergeIter[T constraints.Ordered](items []T) []T {
for step := 1; step < len(items); step += step {
for i := 0; i+step < len(items); i += 2 * step {
tmp := merge(items[i:i+step], items[i+step:min.Int(i+2*step, len(items))])
copy(items[i:], tmp)
}
}
return items
}
// ParallelMerge Perform merge sort on a slice using goroutines
func ParallelMerge[T constraints.Ordered](items []T) []T {
if len(items) < 2 {
return items
}
if len(items) < 2048 {
return Merge(items)
}
var wg sync.WaitGroup
wg.Add(1)
var middle = len(items) / 2
var a []T
go func() {
defer wg.Done()
a = ParallelMerge(items[:middle])
}()
var b = ParallelMerge(items[middle:])
wg.Wait()
return merge(a, b)
}
================================================
FILE: sort/oddevensort.go
================================================
// oddevensort.go
// Implementation of Odd-Even Sort (Brick Sort)
// Reference: https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
package sort
import "github.com/TheAlgorithms/Go/constraints"
// OddEvenSort performs the odd-even sort algorithm on the given array.
// It is a variation of bubble sort that compares adjacent pairs, alternating
// between odd and even indexed elements in each pass until the array is sorted.
func OddEvenSort[T constraints.Ordered](arr []T) []T {
if len(arr) == 0 { // handle empty array
return arr
}
swapped := true
for swapped {
swapped = false
// Perform "odd" indexed pass
for i := 1; i < len(arr)-1; i += 2 {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true
}
}
// Perform "even" indexed pass
for i := 0; i < len(arr)-1; i += 2 {
if arr[i] > arr[i+1] {
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true
}
}
}
return arr
}
================================================
FILE: sort/pancakesort.go
================================================
package sort
import "github.com/TheAlgorithms/Go/constraints"
// Pancake sorts a slice using flip operations,
// where flip refers to the idea of reversing the
// slice from index `0` to `i`.
func Pancake[T constraints.Ordered](arr []T) []T {
// early return if the array too small
if len(arr) <= 1 {
return arr
}
// start from the end of the array
for i := len(arr) - 1; i > 0; i-- {
// find the index of the maximum element in arr
max := 0
for j := 1; j <= i; j++ {
if arr[j] > arr[max] {
max = j
}
}
// if the maximum element is not at the end of the array
if max != i {
// flip the maximum element to the beginning of the array
arr = flip(arr, max)
// flip the maximum element to the end of the array by flipping the whole array
arr = flip(arr, i)
}
}
return arr
}
// flip reverses the input slice from `0` to `i`.
func flip[T constraints.Ordered](arr []T, i int) []T {
for j := 0; j < i; j++ {
arr[j], arr[i] = arr[i], arr[j]
i--
}
return arr
}
================================================
FILE: sort/patiencesort.go
================================================
// Patience sorting is a sorting algorithm inspired by the card game patience.
//
// For more details check out those links below here:
// GeeksForGeeks article : https://www.geeksforgeeks.org/patience-sorting/
// Wikipedia article: https://en.wikipedia.org/wiki/Patience_sorting
// authors [guuzaa](https://github.com/guuzaa)
// worst-case time complexity: O(n log n)
// average time complexity: O(n log n)
// space complexity: O(n)
// see patiencesort.go
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Patience[T constraints.Ordered](arr []T) []T {
if len(arr) <= 1 {
return arr
}
var piles [][]T
for _, card := range arr {
left, right := 0, len(piles)
for left < right {
mid := left + (right-left)/2
if piles[mid][len(piles[mid])-1] >= card {
right = mid
} else {
left = mid + 1
}
}
if left == len(piles) {
piles = append(piles, []T{card})
} else {
piles[left] = append(piles[left], card)
}
}
return mergePiles(piles)
}
func mergePiles[T constraints.Ordered](piles [][]T) []T {
var ret []T
for len(piles) > 0 {
minID := 0
minValue := piles[minID][len(piles[minID])-1]
for i := 1; i < len(piles); i++ {
if minValue <= piles[i][len(piles[i])-1] {
continue
}
minValue = piles[i][len(piles[i])-1]
minID = i
}
ret = append(ret, minValue)
piles[minID] = piles[minID][:len(piles[minID])-1]
if len(piles[minID]) == 0 {
piles = append(piles[:minID], piles[minID+1:]...)
}
}
return ret
}
================================================
FILE: sort/pigeonholesort.go
================================================
// Pigeonhole algorithm's working at wikipedia.
// https://en.wikipedia.org/wiki/Pigeonhole_sort
// time complexity: O(n + N) where n is the number of elements in the array and N is the range of input
// space complexity: O(N)
package sort
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/max"
"github.com/TheAlgorithms/Go/math/min"
)
// Pigeonhole sorts a slice using pigeonhole sorting algorithm.
// NOTE: To maintain time complexity O(n + N), this is the reason for having
// only Integer constraint instead of Ordered.
func Pigeonhole[T constraints.Integer](arr []T) []T {
if len(arr) == 0 {
return arr
}
max := max.Int(arr...)
min := min.Int(arr...)
size := max - min + 1
holes := make([]T, size)
for _, element := range arr {
holes[element-min]++
}
i := 0
for j := T(0); j < size; j++ {
for holes[j] > 0 {
holes[j]--
arr[i] = j + min
i++
}
}
return arr
}
================================================
FILE: sort/quicksort.go
================================================
// quicksort.go
// description: Implementation of in-place quicksort algorithm
// details:
// A simple in-place quicksort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Quicksort)
// worst time complexity: O(n^2)
// average time complexity: O(n log n)
// space complexity: O(log n)
// author(s) [Taj](https://github.com/tjgurwara99)
// see sort_test.go for a test implementation, test function TestQuickSort.
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Partition[T constraints.Ordered](arr []T, low, high int) int {
index := low - 1
pivotElement := arr[high]
for i := low; i < high; i++ {
if arr[i] <= pivotElement {
index += 1
arr[index], arr[i] = arr[i], arr[index]
}
}
arr[index+1], arr[high] = arr[high], arr[index+1]
return index + 1
}
// QuicksortRange Sorts the specified range within the array
func QuicksortRange[T constraints.Ordered](arr []T, low, high int) {
if len(arr) <= 1 {
return
}
if low < high {
pivot := Partition(arr, low, high)
QuicksortRange(arr, low, pivot-1)
QuicksortRange(arr, pivot+1, high)
}
}
// Quicksort Sorts the entire array
func Quicksort[T constraints.Ordered](arr []T) []T {
QuicksortRange(arr, 0, len(arr)-1)
return arr
}
================================================
FILE: sort/radixsort.go
================================================
// radixsort.go
// description: Implementation of in-place radixsort algorithm
// details:
// A simple in-place quicksort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Radix_sort)
// worst time complexity: O(n * k) where n is the number of elements in the input array and k is the number of digits in the largest number
// average time complexity: O(n * k) where n is the number of elements in the input array and k is the number of digits in the largest number
// space complexity: O(n)
package sort
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/max"
)
func countSort[T constraints.Integer](arr []T, exp T) []T {
var digits [10]int
var output = make([]T, len(arr))
for _, item := range arr {
digits[(item/exp)%10]++
}
for i := 1; i < 10; i++ {
digits[i] += digits[i-1]
}
for i := len(arr) - 1; i >= 0; i-- {
output[digits[(arr[i]/exp)%10]-1] = arr[i]
digits[(arr[i]/exp)%10]--
}
return output
}
func unsignedRadixSort[T constraints.Integer](arr []T) []T {
if len(arr) == 0 {
return arr
}
maxElement := max.Int(arr...)
for exp := T(1); maxElement/exp > 0; exp *= 10 {
arr = countSort(arr, exp)
}
return arr
}
func RadixSort[T constraints.Integer](arr []T) []T {
if len(arr) < 1 {
return arr
}
var negatives, nonNegatives []T
for _, item := range arr {
if item < 0 {
negatives = append(negatives, -item)
} else {
nonNegatives = append(nonNegatives, item)
}
}
negatives = unsignedRadixSort(negatives)
// Reverse the negative array and restore signs
for i, j := 0, len(negatives)-1; i <= j; i, j = i+1, j-1 {
negatives[i], negatives[j] = -negatives[j], -negatives[i]
}
return append(negatives, unsignedRadixSort(nonNegatives)...)
}
================================================
FILE: sort/selectionsort.go
================================================
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Selection[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr); i++ {
min := i
for j := i + 1; j < len(arr); j++ {
if arr[j] < arr[min] {
min = j
}
}
arr[i], arr[min] = arr[min], arr[i]
}
return arr
}
================================================
FILE: sort/shellsort.go
================================================
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Shell[T constraints.Ordered](arr []T) []T {
for d := int(len(arr) / 2); d > 0; d /= 2 {
for i := d; i < len(arr); i++ {
for j := i; j >= d && arr[j-d] > arr[j]; j -= d {
arr[j], arr[j-d] = arr[j-d], arr[j]
}
}
}
return arr
}
================================================
FILE: sort/simplesort.go
================================================
// simplesort.go
// description: Implementation of a simple sorting algorithm
// details:
// A simple sorting algorithm that look counter intuitive at first glance and very similar to Exchange Sort
// An improved version is included with slight changes to make the sort slightly more efficient
// reference: https://arxiv.org/abs/2110.01111v1
// see sort_test.go for a test implementation, test function TestSimple and TestImprovedSimple
// worst-case time complexity: O(n^2)
// average-case time complexity: O(n^2)
// space complexity: O(1)
package sort
import "github.com/TheAlgorithms/Go/constraints"
func Simple[T constraints.Ordered](arr []T) []T {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr); j++ {
if arr[i] < arr[j] {
// swap arr[i] and arr[j]
arr[i], arr[j] = arr[j], arr[i]
}
}
}
return arr
}
// ImprovedSimple is a improve SimpleSort by skipping an unnecessary comparison of the first and last.
// This improved version is more similar to implementation of insertion sort
func ImprovedSimple[T constraints.Ordered](arr []T) []T {
for i := 1; i < len(arr); i++ {
for j := 0; j < len(arr)-1; j++ {
if arr[i] < arr[j] {
// swap arr[i] and arr[j]
arr[i], arr[j] = arr[j], arr[i]
}
}
}
return arr
}
================================================
FILE: sort/sorts_test.go
================================================
package sort_test
import (
"math/rand"
"reflect"
"testing"
"time"
"github.com/TheAlgorithms/Go/sort"
)
func testFramework(t *testing.T, sortingFunction func([]int) []int) {
sortTests := []struct {
input []int
expected []int
name string
}{
//Sorted slice
{
input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
expected: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
name: "Sorted Unsigned",
},
//Reversed slice
{
input: []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
expected: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
name: "Reversed Unsigned",
},
//Sorted slice
{
input: []int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
expected: []int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
name: "Sorted Signed",
},
//Reversed slice
{
input: []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10},
expected: []int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
name: "Reversed Signed",
},
//Reversed slice, even length
{
input: []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10},
expected: []int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
name: "Reversed Signed #2",
},
//Random order with repetitions
{
input: []int{-5, 7, 4, -2, 6, 5, 8, 3, 2, -7, -1, 0, -3, 9, -6, -4, 10, 9, 1, -8, -9, -10},
expected: []int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10},
name: "Random order Signed",
},
//Single-entry slice
{
input: []int{1},
expected: []int{1},
name: "Singleton",
},
// Empty slice
{
input: []int{},
expected: []int{},
name: "Empty Slice",
},
}
for _, test := range sortTests {
t.Run(test.name, func(t *testing.T) {
actual := sortingFunction(test.input)
sorted := reflect.DeepEqual(actual, test.expected)
if !sorted {
t.Errorf("test %s failed", test.name)
t.Errorf("actual %v expected %v", actual, test.expected)
}
})
}
}
// BEGIN TESTS
func TestBinaryInsertion(t *testing.T) {
testFramework(t, sort.BinaryInsertion[int])
}
func TestBubble(t *testing.T) {
testFramework(t, sort.Bubble[int])
}
func TestBogo(t *testing.T) {
t.Skip("Skipping test for Bogo Sort, as it uses a lot of resource.")
testFramework(t, sort.Bogo[int])
}
func TestBucketSort(t *testing.T) {
testFramework(t, sort.Bucket[int])
}
func TestCocktailSort(t *testing.T) {
testFramework(t, sort.Cocktail[int])
}
func TestExchange(t *testing.T) {
testFramework(t, sort.Exchange[int])
}
func TestInsertion(t *testing.T) {
testFramework(t, sort.Insertion[int])
}
func TestMerge(t *testing.T) {
testFramework(t, sort.Merge[int])
}
func TestMergeIter(t *testing.T) {
testFramework(t, sort.MergeIter[int])
}
func TestMergeParallel(t *testing.T) {
testFramework(t, sort.ParallelMerge[int])
// Test parallel merge sort with a large slice
t.Run("ParallelMerge on large slice", func(t *testing.T) {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
size := 100000
randomLargeSlice := make([]int, size)
for i := range randomLargeSlice {
randomLargeSlice[i] = rnd.Intn(size)
}
sortedSlice := sort.ParallelMerge[int](randomLargeSlice)
for i := 0; i < len(sortedSlice)-1; i++ {
if sortedSlice[i] > sortedSlice[i+1] {
t.Errorf("ParallelMerge failed")
}
}
})
}
func TestHeap(t *testing.T) {
testFramework(t, sort.HeapSort[int])
}
func TestCount(t *testing.T) {
testFramework(t, sort.Count[int])
}
func TestQuick(t *testing.T) {
testFramework(t, sort.Quicksort[int])
}
func TestShell(t *testing.T) {
testFramework(t, sort.Shell[int])
}
func TestRadix(t *testing.T) {
testFramework(t, sort.RadixSort[int])
}
func TestSimple(t *testing.T) {
testFramework(t, sort.Simple[int])
}
func TestImprovedSimple(t *testing.T) {
testFramework(t, sort.ImprovedSimple[int])
}
func TestSelection(t *testing.T) {
testFramework(t, sort.Selection[int])
}
func TestComb(t *testing.T) {
testFramework(t, sort.Comb[int])
}
func TestPancakeSort(t *testing.T) {
testFramework(t, sort.Pancake[int])
}
func TestPigeonhole(t *testing.T) {
testFramework(t, sort.Pigeonhole[int])
}
func TestPatience(t *testing.T) {
testFramework(t, sort.Patience[int])
}
func TestCycle(t *testing.T) {
testFramework(t, sort.Cycle[int])
}
func TestTimsort(t *testing.T) {
testFramework(t, sort.Timsort[int])
}
func TestCircle(t *testing.T) {
testFramework(t, sort.Circle[int])
}
func TestOddEvenSort(t *testing.T) {
testFramework(t, sort.OddEvenSort[int])
}
func TestStooge(t *testing.T) {
testFramework(t, sort.Stooge[int])
}
// END TESTS
func benchmarkFramework(b *testing.B, f func(arr []int) []int) {
var sortTests = []struct {
input []int
expected []int
name string
}{
//Sorted slice
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Sorted Unsigned"},
//Reversed slice
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Unsigned"},
//Sorted slice
{[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Sorted Signed"},
//Reversed slice
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10},
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Signed"},
//Reversed slice, even length
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10},
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "Reversed Signed #2"},
//Random order with repetitions
{[]int{-5, 7, 4, -2, 6, 5, 8, 3, 2, -7, -1, 0, -3, 9, -6, -4, 10, 9, 1, -8, -9, -10},
[]int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10}, "Random order Signed"},
//Empty slice
{[]int{}, []int{}, "Empty"},
//Single-entry slice
{[]int{1}, []int{1}, "Singleton"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
f(test.input)
}
}
}
//BEGIN BENCHMARKS
func BenchmarkBinaryInsertion(b *testing.B) {
benchmarkFramework(b, sort.BinaryInsertion[int])
}
func BenchmarkBubble(b *testing.B) {
benchmarkFramework(b, sort.Bubble[int])
}
func BenchmarkBogo(b *testing.B) {
b.Skip("Skipping benchmark for Bogo Sort, as it uses a lot of resource.")
benchmarkFramework(b, sort.Bogo[int])
}
func BenchmarkBucketSort(b *testing.B) {
benchmarkFramework(b, sort.Bucket[int])
}
func BenchmarkCocktailSort(b *testing.B) {
benchmarkFramework(b, sort.Cocktail[int])
}
func BenchmarkExchange(b *testing.B) {
benchmarkFramework(b, sort.Exchange[int])
}
func BenchmarkInsertion(b *testing.B) {
benchmarkFramework(b, sort.Insertion[int])
}
func BenchmarkMerge(b *testing.B) {
benchmarkFramework(b, sort.Merge[int])
}
func BenchmarkMergeIter(b *testing.B) {
benchmarkFramework(b, sort.MergeIter[int])
}
func BenchmarkMergeParallel(b *testing.B) {
benchmarkFramework(b, sort.ParallelMerge[int])
}
func BenchmarkHeap(b *testing.B) {
benchmarkFramework(b, sort.HeapSort[int])
}
func BenchmarkCount(b *testing.B) {
benchmarkFramework(b, sort.Count[int])
}
func BenchmarkQuick(b *testing.B) {
benchmarkFramework(b, sort.Quicksort[int])
}
func BenchmarkShell(b *testing.B) {
benchmarkFramework(b, sort.Shell[int])
}
func BenchmarkRadix(b *testing.B) {
benchmarkFramework(b, sort.RadixSort[int])
}
func BenchmarkSimple(b *testing.B) {
benchmarkFramework(b, sort.Simple[int])
}
func BenchmarkImprovedSimple(b *testing.B) {
benchmarkFramework(b, sort.ImprovedSimple[int])
}
// Very Slow, consider commenting
func BenchmarkSelection(b *testing.B) {
benchmarkFramework(b, sort.Selection[int])
}
func BenchmarkComb(b *testing.B) {
benchmarkFramework(b, sort.Comb[int])
}
func BenchmarkPancakeSort(b *testing.B) {
benchmarkFramework(b, sort.Pancake[int])
}
func BenchmarkPigeonhole(b *testing.B) {
benchmarkFramework(b, sort.Pigeonhole[int])
}
func BenchmarkPatience(b *testing.B) {
benchmarkFramework(b, sort.Patience[int])
}
func BenchmarkCycle(b *testing.B) {
benchmarkFramework(b, sort.Cycle[int])
}
func BenchmarkTimsort(b *testing.B) {
benchmarkFramework(b, sort.Timsort[int])
}
func BenchmarkCircle(b *testing.B) {
benchmarkFramework(b, sort.Circle[int])
}
func BenchmarkStooge(b *testing.B) {
benchmarkFramework(b, sort.Stooge[int])
}
================================================
FILE: sort/stooge_sort.go
================================================
// implementation of the Stooge sort
// more info at https://en.wikipedia.org/wiki/Stooge_sort
// worst-case time complexity O(n^2.709511)
// worst-case space complexity O(n)
package sort
import (
"github.com/TheAlgorithms/Go/constraints"
// math imported for floor division
"math"
)
func innerStooge[T constraints.Ordered](arr []T, i int32, j int32) []T {
if arr[i] > arr[j] {
arr[i], arr[j] = arr[j], arr[i]
}
if (j - i + 1) > 2 {
t := int32(math.Floor(float64(j-i+1) / 3.0))
arr = innerStooge(arr, i, j-t)
arr = innerStooge(arr, i+t, j)
arr = innerStooge(arr, i, j-t)
}
return arr
}
func Stooge[T constraints.Ordered](arr []T) []T {
if len(arr) == 0 {
return arr
}
return innerStooge(arr, 0, int32(len(arr)-1))
}
================================================
FILE: sort/timsort.go
================================================
// Implementation of Timsort algorithm
// Reference: https://en.wikipedia.org/wiki/Timsort
package sort
import (
"github.com/TheAlgorithms/Go/constraints"
)
const runSizeThreshold = 8
// Timsort is a simple generic implementation of Timsort algorithm.
func Timsort[T constraints.Ordered](data []T) []T {
runSize := calculateRunSize(len(data))
insertionSortRuns(data, runSize)
mergeRuns(data, runSize)
return data
}
// calculateRunSize returns a run size parameter that is further used
// to slice the data slice.
func calculateRunSize(dataLength int) int {
remainder := 0
for dataLength >= runSizeThreshold {
if dataLength%2 == 1 {
remainder = 1
}
dataLength = dataLength / 2
}
return dataLength + remainder
}
// insertionSortRuns runs insertion sort on all the data runs one by one.
func insertionSortRuns[T constraints.Ordered](data []T, runSize int) {
for lower := 0; lower < len(data); lower += runSize {
upper := lower + runSize
if upper >= len(data) {
upper = len(data)
}
Insertion(data[lower:upper])
}
}
// mergeRuns merge sorts all the data runs into a single sorted data slice.
func mergeRuns[T constraints.Ordered](data []T, runSize int) {
for size := runSize; size < len(data); size *= 2 {
for lowerBound := 0; lowerBound < len(data); lowerBound += size * 2 {
middleBound := lowerBound + size - 1
upperBound := lowerBound + 2*size - 1
if len(data)-1 < upperBound {
upperBound = len(data) - 1
}
mergeRun(data, lowerBound, middleBound, upperBound)
}
}
}
// mergeRun uses merge sort to sort adjacent data runs.
func mergeRun[T constraints.Ordered](data []T, lower, mid, upper int) {
left := data[lower : mid+1]
right := data[mid+1 : upper+1]
merged := merge(left, right)
// rewrite original data slice values with sorted values from merged slice
for i, value := range merged {
data[lower+i] = value
}
}
================================================
FILE: sqrt/sqrtdecomposition.go
================================================
// Package sqrt contains algorithms and data structures that contains a √n in their complexity
package sqrt
import "math"
// Sqrt (or Square Root) Decomposition is a technique used for query an array and perform updates
// Inside this package is described its most simple data structure, you can find more at: https://cp-algorithms.com/data_structures/sqrt_decomposition.html
//
// Formally, You can use SqrtDecomposition only if:
//
// Given a function $Query:E_1,...,E_n\rightarrow Q$
//
// if $\exist unionQ:Q,Q\rightarrow Q$
//
// s.t.
//
// - $\forall n\in \N > 1, 1\le i 0, E_1,..., E_n\in E \\ query(E_1,...,E_{new},..., E_n)=updateQ(query(E_1,...,E_{old},...,E_n), indexof(E_{old}), E_{new})$
type SqrtDecomposition[E any, Q any] struct {
querySingleElement func(element E) Q
unionQ func(q1 Q, q2 Q) Q
updateQ func(oldQ Q, oldE E, newE E) (newQ Q)
elements []E
blocks []Q
blockSize uint64
}
// Create a new SqrtDecomposition instance with the parameters as specified by SqrtDecomposition comment
// Assumptions:
// - len(elements) > 0
func NewSqrtDecomposition[E any, Q any](
elements []E,
querySingleElement func(element E) Q,
unionQ func(q1 Q, q2 Q) Q,
updateQ func(oldQ Q, oldE E, newE E) (newQ Q),
) *SqrtDecomposition[E, Q] {
sqrtDec := &SqrtDecomposition[E, Q]{
querySingleElement: querySingleElement,
unionQ: unionQ,
updateQ: updateQ,
elements: elements,
}
sqrt := math.Sqrt(float64(len(sqrtDec.elements)))
blockSize := uint64(sqrt)
numBlocks := uint64(math.Ceil(float64(len(elements)) / float64(blockSize)))
sqrtDec.blocks = make([]Q, numBlocks)
for i := uint64(0); i < uint64(len(elements)); i++ {
if i%blockSize == 0 {
sqrtDec.blocks[i/blockSize] = sqrtDec.querySingleElement(elements[i])
} else {
sqrtDec.blocks[i/blockSize] = sqrtDec.unionQ(sqrtDec.blocks[i/blockSize], sqrtDec.querySingleElement(elements[i]))
}
}
sqrtDec.blockSize = blockSize
return sqrtDec
}
// Performs a query from index start to index end (non included)
// Assumptions:
// - start < end
// - start and end are valid
func (s *SqrtDecomposition[E, Q]) Query(start uint64, end uint64) Q {
firstIndexNextBlock := ((start / s.blockSize) + 1) * s.blockSize
q := s.querySingleElement(s.elements[start])
if firstIndexNextBlock > end { // if in same block
start++
for start < end {
q = s.unionQ(q, s.querySingleElement(s.elements[start]))
start++
}
} else {
// left side
start++
for start < firstIndexNextBlock {
q = s.unionQ(q, s.querySingleElement(s.elements[start]))
start++
}
//middle part
endBlock := end / s.blockSize
for i := firstIndexNextBlock / s.blockSize; i < endBlock; i++ {
q = s.unionQ(q, s.blocks[i])
}
// right part
for i := endBlock * s.blockSize; i < end; i++ {
q = s.unionQ(q, s.querySingleElement(s.elements[i]))
}
}
return q
}
// Assumptions:
// - index is valid
func (s *SqrtDecomposition[E, Q]) Update(index uint64, newElement E) {
i := index / s.blockSize
s.blocks[i] = s.updateQ(s.blocks[i], s.elements[index], newElement)
s.elements[index] = newElement
}
================================================
FILE: sqrt/sqrtdecomposition_test.go
================================================
package sqrt_test
import (
"github.com/TheAlgorithms/Go/sqrt"
"testing"
)
// Query interval
type query struct {
firstIndex uint64
lastIndex uint64
}
type update struct {
index uint64
value int
}
func TestSqrtDecomposition(t *testing.T) {
var sqrtDecompositionTestData = []struct {
description string
array []int
updates []update
queries []query
expected []int
}{
{
description: "test 1-sized array",
array: []int{1},
queries: []query{{0, 1}},
expected: []int{1},
},
{
description: "test array with size 5",
array: []int{1, 2, 3, 4, 5},
queries: []query{{0, 5}, {0, 2}, {2, 4}},
expected: []int{15, 3, 7},
},
{
description: "test array with size 5 and updates",
array: []int{1, 2, 3, 4, 5},
updates: []update{{index: 1, value: 3},
{index: 2, value: 4}},
queries: []query{{0, 5}, {0, 2}, {2, 4}},
expected: []int{17, 4, 8},
},
{
description: "test array with size 11 and updates",
array: []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
updates: []update{{index: 2, value: 2},
{index: 3, value: 3},
{index: 6, value: 6}},
queries: []query{{3, 5}, {7, 8}, {3, 7}, {0, 10}},
expected: []int{4, 1, 11, 18},
},
}
for _, test := range sqrtDecompositionTestData {
t.Run(test.description, func(t *testing.T) {
s := sqrt.NewSqrtDecomposition(test.array,
func(e int) int { return e },
func(q1, q2 int) int { return q1 + q2 },
func(q, a, b int) int { return q - a + b },
)
for i := 0; i < len(test.updates); i++ {
s.Update(test.updates[i].index, test.updates[i].value)
}
for i := 0; i < len(test.queries); i++ {
result := s.Query(test.queries[i].firstIndex, test.queries[i].lastIndex)
if result != test.expected[i] {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expected result: %d\nFound: %d\n", test.expected[i], result)
}
}
})
}
}
================================================
FILE: strings/ahocorasick/advancedahocorasick.go
================================================
package ahocorasick
import (
"fmt"
"time"
)
// Advanced Function performing the Advanced Aho-Corasick algorithm.
// Finds and prints occurrences of each pattern.
func Advanced(t string, p []string) Result {
startTime := time.Now()
occurrences := make(map[int][]int)
ac, f := BuildExtendedAc(p)
current := 0
for pos := 0; pos < len(t); pos++ {
if GetTransition(current, t[pos], ac) != -1 {
current = GetTransition(current, t[pos], ac)
} else {
current = 0
}
_, ok := f[current]
if ok {
for i := range f[current] {
if p[f[current][i]] == GetWord(pos-len(p[f[current][i]])+1, pos, t) { //check for word match
newOccurrences := IntArrayCapUp(occurrences[f[current][i]])
occurrences[f[current][i]] = newOccurrences
occurrences[f[current][i]][len(newOccurrences)-1] = pos - len(p[f[current][i]]) + 1
}
}
}
}
elapsed := time.Since(startTime)
fmt.Printf("\n\nElapsed %f secs\n", elapsed.Seconds())
var resultOccurrences = make(map[string][]int)
for key, value := range occurrences {
resultOccurrences[p[key]] = value
}
return Result{
resultOccurrences,
}
}
// BuildExtendedAc Functions that builds extended Aho Corasick automaton.
func BuildExtendedAc(p []string) (acToReturn map[int]map[uint8]int, f map[int][]int) {
acTrie, stateIsTerminal, f := ConstructTrie(p)
s := make([]int, len(stateIsTerminal)) //supply function
i := 0 //root of acTrie
acToReturn = acTrie
s[i] = -1
for current := 1; current < len(stateIsTerminal); current++ {
o, parent := GetParent(current, acTrie)
down := s[parent]
for StateExists(down, acToReturn) && GetTransition(down, o, acToReturn) == -1 {
down = s[down]
}
if StateExists(down, acToReturn) {
s[current] = GetTransition(down, o, acToReturn)
if stateIsTerminal[s[current]] {
stateIsTerminal[current] = true
f[current] = ArrayUnion(f[current], f[s[current]]) //F(Current) <- F(Current) union F(S(Current))
}
} else {
s[current] = i //initial state?
}
}
a := ComputeAlphabet(p) // concat of all patterns in p
for j := range a {
if GetTransition(i, a[j], acToReturn) == -1 {
CreateTransition(i, a[j], i, acToReturn)
}
}
for current := 1; current < len(stateIsTerminal); current++ {
for j := range a {
if GetTransition(current, a[j], acToReturn) == -1 {
CreateTransition(current, a[j], GetTransition(s[current], a[j], acToReturn), acToReturn)
}
}
}
return acToReturn, f
}
================================================
FILE: strings/ahocorasick/advancedahocorasick_test.go
================================================
package ahocorasick
import (
"reflect"
"testing"
)
var testCases = []struct {
name string
words []string
text string
expected Result
}{
{
"String comparison on all patterns found",
[]string{"announce", "annual", "annually"},
"CPM_annual_conferenceannounce_announce_annually_announce",
Result{
map[string][]int{
"annual": {4, 39},
"announce": {21, 30, 48},
"annually": {39},
},
},
},
{
"String comparison on not all patterns found",
[]string{"announce", "annual", "annually"},
"CPM_annual_conference_announce",
Result{
map[string][]int{
"annual": {4},
"announce": {22},
},
},
},
{
"String comparison on not all patterns found",
[]string{"announce", "annual", "annually"},
"CPM_annual_conference_announce",
Result{
map[string][]int{
"annual": {4},
"announce": {22},
},
},
},
}
func TestAdvanced(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := AhoCorasick(tc.text, tc.words)
if !reflect.DeepEqual(actual, tc.expected) {
actualString := convertToString(actual)
expectedString := convertToString(tc.expected)
t.Errorf("Expected matches for patterns %s for string '%s' are: patterns and positions found %v, but actual matches are: patterns and positions found %v",
tc.words, tc.text, actualString, expectedString)
}
})
}
}
================================================
FILE: strings/ahocorasick/ahocorasick.go
================================================
package ahocorasick
import (
"fmt"
"time"
)
// Result structure to hold occurrences
type Result struct {
occurrences map[string][]int
}
// AhoCorasick Function performing the Basic Aho-Corasick algorithm.
// Finds and prints occurrences of each pattern.
func AhoCorasick(t string, p []string) Result {
startTime := time.Now()
occurrences := make(map[int][]int)
ac, f, s := BuildAc(p)
current := 0
for pos := 0; pos < len(t); pos++ {
for GetTransition(current, t[pos], ac) == -1 && s[current] != -1 {
current = s[current]
}
if GetTransition(current, t[pos], ac) != -1 {
current = GetTransition(current, t[pos], ac)
fmt.Printf(" (Continue) \n")
} else {
current = 0
}
_, ok := f[current]
if ok {
for i := range f[current] {
if p[f[current][i]] == GetWord(pos-len(p[f[current][i]])+1, pos, t) { //check for word match
newOccurrences := IntArrayCapUp(occurrences[f[current][i]])
occurrences[f[current][i]] = newOccurrences
occurrences[f[current][i]][len(newOccurrences)-1] = pos - len(p[f[current][i]]) + 1
}
}
}
}
elapsed := time.Since(startTime)
fmt.Printf("\n\nElapsed %f secs\n", elapsed.Seconds())
var resultOccurrences = make(map[string][]int)
for key, value := range occurrences {
resultOccurrences[p[key]] = value
}
return Result{
resultOccurrences,
}
}
// Functions that builds Aho Corasick automaton.
func BuildAc(p []string) (acToReturn map[int]map[uint8]int, f map[int][]int, s []int) {
acTrie, stateIsTerminal, f := ConstructTrie(p)
s = make([]int, len(stateIsTerminal)) //supply function
i := 0 //root of acTrie
acToReturn = acTrie
s[i] = -1
for current := 1; current < len(stateIsTerminal); current++ {
o, parent := GetParent(current, acTrie)
down := s[parent]
for StateExists(down, acToReturn) && GetTransition(down, o, acToReturn) == -1 {
down = s[down]
}
if StateExists(down, acToReturn) {
s[current] = GetTransition(down, o, acToReturn)
if stateIsTerminal[s[current]] {
stateIsTerminal[current] = true
f[current] = ArrayUnion(f[current], f[s[current]]) //F(Current) <- F(Current) union F(S(Current))
}
} else {
s[current] = i //initial state?
}
}
return acToReturn, f, s
}
================================================
FILE: strings/ahocorasick/ahocorasick_test.go
================================================
package ahocorasick
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestAhoCorasick(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := AhoCorasick(tc.text, tc.words)
if !reflect.DeepEqual(actual, tc.expected) {
actualString := convertToString(actual)
expectedString := convertToString(tc.expected)
t.Errorf("Expected matches for patterns %s for string '%s' are: patterns and positions found %v, but actual matches are: patterns and positions found %v",
tc.words, tc.text, actualString, expectedString)
}
})
}
}
func convertToString(res Result) string {
var r strings.Builder
for key, val := range res.occurrences {
r.WriteString(fmt.Sprintf("Word: '%s' at positions: ", key))
for i := range val {
r.WriteString(fmt.Sprintf("%d", val[i]))
if i != len(val)-1 {
r.WriteString(", ")
}
}
r.WriteString(". ")
}
return r.String()
}
================================================
FILE: strings/ahocorasick/patterns.txt
================================================
announce annual annually
================================================
FILE: strings/ahocorasick/shared.go
================================================
package ahocorasick
// ConstructTrie Function that constructs Trie as an automaton for a set of reversed & trimmed strings.
func ConstructTrie(p []string) (trie map[int]map[uint8]int, stateIsTerminal []bool, f map[int][]int) {
trie = make(map[int]map[uint8]int)
stateIsTerminal = make([]bool, 1)
f = make(map[int][]int)
state := 1
CreateNewState(0, trie)
for i := 0; i < len(p); i++ {
current := 0
j := 0
for j < len(p[i]) && GetTransition(current, p[i][j], trie) != -1 {
current = GetTransition(current, p[i][j], trie)
j++
}
for j < len(p[i]) {
stateIsTerminal = BoolArrayCapUp(stateIsTerminal)
CreateNewState(state, trie)
stateIsTerminal[state] = false
CreateTransition(current, p[i][j], state, trie)
current = state
j++
state++
}
if stateIsTerminal[current] {
newArray := IntArrayCapUp(f[current])
newArray[len(newArray)-1] = i
f[current] = newArray // F(Current) <- F(Current) union {i}
} else {
stateIsTerminal[current] = true
f[current] = []int{i} // F(Current) <- {i}
}
}
return trie, stateIsTerminal, f
}
// Contains Returns 'true' if array of int's 's' contains int 'e', 'false' otherwise.
func Contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// GetWord Function that returns word found in text 't' at position range 'begin' to 'end'.
func GetWord(begin, end int, t string) string {
for end >= len(t) {
return ""
}
d := make([]uint8, end-begin+1)
for j, i := 0, begin; i <= end; i, j = i+1, j+1 {
d[j] = t[i]
}
return string(d)
}
// ComputeAlphabet Function that returns string of all the possible characters in given patterns.
func ComputeAlphabet(p []string) (s string) {
s = p[0]
for i := 1; i < len(p); i++ {
s = s + p[i]
}
return s
}
// IntArrayCapUp Dynamically increases an array size of int's by 1.
func IntArrayCapUp(old []int) (new []int) {
new = make([]int, cap(old)+1)
copy(new, old) //copy(dst,src)
// old = new
return new
}
// BoolArrayCapUp Dynamically increases an array size of bool's by 1.
func BoolArrayCapUp(old []bool) (new []bool) {
new = make([]bool, cap(old)+1)
copy(new, old)
// old = new
return new
}
// ArrayUnion Concats two arrays of int's into one.
func ArrayUnion(to, from []int) (concat []int) {
concat = to
for i := range from {
if !Contains(concat, from[i]) {
concat = IntArrayCapUp(concat)
concat[len(concat)-1] = from[i]
}
}
return concat
}
// GetParent Function that finds the first previous state of a state and returns it.
// Used for trie where there is only one parent.
func GetParent(state int, at map[int]map[uint8]int) (uint8, int) {
for beginState, transitions := range at {
for c, endState := range transitions {
if endState == state {
return c, beginState
}
}
}
return 0, 0 //unreachable
}
// CreateNewState Automaton function for creating a new state 'state'.
func CreateNewState(state int, at map[int]map[uint8]int) {
at[state] = make(map[uint8]int)
}
// CreateTransition Creates a transition for function σ(state,letter) = end.
func CreateTransition(fromState int, overChar uint8, toState int, at map[int]map[uint8]int) {
at[fromState][overChar] = toState
}
// GetTransition Returns ending state for transition σ(fromState,overChar), '-1' if there is none.
func GetTransition(fromState int, overChar uint8, at map[int]map[uint8]int) (toState int) {
if !StateExists(fromState, at) {
return -1
}
toState, ok := at[fromState][overChar]
if !ok {
return -1
}
return toState
}
// StateExists Checks if state 'state' exists. Returns 'true' if it does, 'false' otherwise.
func StateExists(state int, at map[int]map[uint8]int) bool {
_, ok := at[state]
if !ok || state == -1 || at[state] == nil {
return false
}
return true
}
================================================
FILE: strings/ahocorasick/text.txt
================================================
CPM_annual_conference_announce
================================================
FILE: strings/bom/bom.go
================================================
package bom
// User defined.
// Set to true to print various extra stuff out (slows down the execution)
// Set to false for quick and quiet execution.
// const debugMode bool = false
// User defined.
// Set to true to read input from two command line arguments
// Set to false to read input from two files "pattern.txt" and "text.txt"
// const commandLineInput bool = false
// Implementation of Backward Oracle Matching algorithm (Factor based approach).
// Requires either a two command line arguments separated by a single space,
// or two files in the same folder: "pattern.txt" containing the string to
// be searched for, "text.txt" containing the text to be searched in.
// func main() {
// if commandLineInput == true { // case of command line input
// args := os.Args
// if len(args) <= 2 {
// log.Fatal("Not enough arguments. Two string arguments separated by spaces are required!")
// }
// pattern := args[1]
// s := args[2]
// for i := 3; i < len(args); i++ {
// s = s + " " + args[i]
// }
// if len(args[1]) > len(s) {
// log.Fatal("Pattern is longer than text!")
// }
// if debugMode == true {
// fmt.Printf("\nRunning: Backward Oracle Matching algorithm.\n\n")
// fmt.Printf("Search word (%d chars long): %q.\n", len(args[1]), pattern)
// fmt.Printf("Text (%d chars long): %q.\n\n", len(s), s)
// } else {
// fmt.Printf("\nRunning: Backward Oracle Matching algorithm.\n\n")
// }
// bom(s, pattern)
// } else if commandLineInput == false { // case of file line input
// patFile, err := ioutil.ReadFile("pattern.txt")
// if err != nil {
// log.Fatal(err)
// }
// textFile, err := ioutil.ReadFile("text.txt")
// if err != nil {
// log.Fatal(err)
// }
// if len(patFile) > len(textFile) {
// log.Fatal("Pattern is longer than text!")
// }
// if debugMode == true {
// fmt.Printf("\nRunning: Backward Oracle Matching algorithm.\n\n")
// fmt.Printf("Search word (%d chars long): %q.\n", len(patFile), patFile)
// fmt.Printf("Text (%d chars long): %q.\n\n", len(textFile), textFile)
// } else {
// fmt.Printf("\nRunning: Backward Oracle Matching algorithm.\n\n")
// }
// bom(string(textFile), string(patFile))
// }
// }
// // Function bom performing the Backward Oracle Matching algorithm.
// // Prints whether the word/pattern was found + positions of possible multiple occurrences
// // or that the word was not found.
// func bom(t, p string) {
// startTime := time.Now()
// n, m := len(t), len(p)
// var current, j, pos int
// oracle := oracleOnLine(reverse(p))
// occurrences := make([]int, len(t))
// currentOcc := 0
// pos = 0
// if debugMode == true {
// fmt.Printf("\n\nWe are reading backwards in %q, searching for %q\n\nat position %d:\n", t, p, pos+m-1)
// }
// for pos <= n-m {
// current = 0 //initial state of the oracle
// j = m
// for j > 0 && stateExists(current, oracle) {
// if debugMode == true {
// prettyPrint(current, j, n, pos, t, oracle)
// }
// current = getTransition(current, t[pos+j-1], oracle)
// j--
// }
// if stateExists(current, oracle) {
// if debugMode == true {
// fmt.Printf(" We got an occurrence!")
// }
// occurrences[currentOcc] = pos
// currentOcc++
// }
// pos = pos + j + 1
// if pos+m-1 < len(t) {
// if debugMode == true {
// fmt.Printf("\n\nposition %d:\n", pos+m-1)
// }
// }
// }
// elapsed := time.Since(startTime)
// fmt.Printf("\n\nElapsed %f secs\n", elapsed.Seconds())
// fmt.Printf("\n\n")
// if currentOcc > 0 {
// fmt.Printf("Word %q was found %d times at positions: ", p, currentOcc)
// for k := 0; k < currentOcc-1; k++ {
// fmt.Printf("%d, ", occurrences[k])
// }
// fmt.Printf("%d", occurrences[currentOcc-1])
// fmt.Printf(".\n")
// }
// if currentOcc == 0 {
// fmt.Printf("\nWord was not found.\n")
// }
// return
// }
// // Construction of the factor oracle automaton for a word p.
// func oracleOnLine(p string) (oracle map[int]map[uint8]int) {
// if debugMode == true {
// fmt.Printf("Oracle construction: \n")
// }
// oracle = make(map[int]map[uint8]int)
// supply := make([]int, len(p)+2) // supply function
// createNewState(0, oracle)
// supply[0] = -1
// var orP string
// for j := 0; j < len(p); j++ {
// oracle, orP = oracleAddLetter(oracle, supply, orP, p[j])
// }
// return oracle
// }
// // Adds one letter to the oracle.
// func oracleAddLetter(oracle map[int]map[uint8]int, supply []int, orP string, o uint8) (oracleToReturn map[int]map[uint8]int, orPToReturn string) {
// m := len(orP)
// var s int
// createNewState(m+1, oracle)
// createTransition(m, o, m+1, oracle)
// k := supply[m]
// for k > -1 && getTransition(k, o, oracle) == -1 {
// createTransition(k, o, m+1, oracle)
// k = supply[k]
// }
// if k == -1 {
// s = 0
// } else {
// s = getTransition(k, o, oracle)
// }
// supply[m+1] = s
// return oracle, orP + string(o)
// }
// // Function that takes a single string and reverses it.
// // @author 'Walter' http://stackoverflow.com/a/10043083
// func reverse(s string) string {
// l := len(s)
// m := make([]rune, l)
// for _, c := range s {
// l--
// m[l] = c
// }
// return string(m)
// }
// // Automaton function for creating a new state.
// func createNewState(state int, at map[int]map[uint8]int) {
// at[state] = make(map[uint8]int)
// if debugMode == true {
// fmt.Printf("\ncreated state %d", state)
// }
// }
// // Creates a transition for function σ(state,letter) = end.
// func createTransition(fromState int, overChar uint8, toState int, at map[int]map[uint8]int) {
// at[fromState][overChar] = toState
// if debugMode == true {
// fmt.Printf("\n σ(%d,%c)=%d;", fromState, overChar, toState)
// }
// }
// // Returns ending state for transition σ(fromState,overChar), -1 if there is none.
// func getTransition(fromState int, overChar uint8, at map[int]map[uint8]int) (toState int) {
// if !stateExists(fromState, at) {
// return -1
// }
// toState, ok := at[fromState][overChar]
// if ok == false {
// return -1
// }
// return toState
// }
// // Checks if state exists. Returns true if it does, false otherwise.
// func stateExists(state int, at map[int]map[uint8]int) bool {
// _, ok := at[state]
// if !ok || state == -1 || at[state] == nil {
// return false
// }
// return true
// }
// // Just some printing of extra information about what the algorithm does.
// func prettyPrint(current int, j int, n int, pos int, t string, oracle map[int]map[uint8]int) {
// if current == 0 && !(getTransition(current, t[pos+j-1], oracle) == -1) {
// fmt.Printf("\n -->(%d)---(%c)--->(%d)", current, t[pos+j-1], getTransition(current, t[pos+j-1], oracle))
// } else if getTransition(current, t[pos+j-1], oracle) == -1 && current != 0 {
// fmt.Printf("\n (%d)---(%c) ", current, t[pos+j-1])
// } else if getTransition(current, t[pos+j-1], oracle) == -1 && current == 0 {
// fmt.Printf("\n -->(%d)---(%c) ", current, t[pos+j-1])
// } else {
// fmt.Printf("\n (%d)---(%c)--->(%d)", current, t[pos+j-1], getTransition(current, t[pos+j-1], oracle))
// }
// fmt.Printf(" ")
// for a := 0; a < pos+j-1; a++ {
// fmt.Printf("%c", t[a])
// }
// if getTransition(current, t[pos+j-1], oracle) == -1 {
// fmt.Printf("[%c]", t[pos+j-1])
// } else {
// fmt.Printf("[%c]", t[pos+j-1])
// }
// for a := pos + j; a < n; a++ {
// fmt.Printf("%c", t[a])
// }
// if getTransition(current, t[pos+j-1], oracle) == -1 {
// fmt.Printf(" FAIL on the character[%c]", t[pos+j-1])
// }
// }
================================================
FILE: strings/charoccurrence.go
================================================
// charoccurrence.go
// description: An algorithm which counts the number
// of times a character occurred in a string.
// author(s) [Moein](https://github.com/mo1ein)
// see charoccurrence_test.go
package strings
// CountChars counts the number of a times a character
// has occurred in the provided string argument and
// returns a map with `rune` as keys and the count as value.
func CountChars(text string) map[rune]int {
charMap := make(map[rune]int, 0)
for _, c := range text {
if _, ok := charMap[c]; !ok {
charMap[c] = 0
}
charMap[c]++
}
return charMap
}
================================================
FILE: strings/charoccurrence_test.go
================================================
package strings_test
import (
"github.com/TheAlgorithms/Go/strings"
"reflect"
"testing"
)
func TestCountChars(t *testing.T) {
var testCases = []struct {
name string
input string
expected map[rune]int
}{
{
"english text",
"hello world!",
map[rune]int{32: 1, 33: 1, 100: 1, 101: 1, 104: 1, 108: 3, 111: 2, 114: 1, 119: 1},
},
{
"chinese text",
" 世界 ",
map[rune]int{32: 2, 19990: 1, 30028: 1},
},
{
"persian text",
"سلام دنیا!",
map[rune]int{32: 1, 33: 1, 1575: 2, 1583: 1, 1587: 1, 1604: 1, 1605: 1, 1606: 1, 1740: 1},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
funcResult := strings.CountChars(test.input)
if !reflect.DeepEqual(test.expected, funcResult) {
t.Errorf("expected: %v, got %v", test.expected, funcResult)
}
})
}
}
================================================
FILE: strings/combination/combination.go
================================================
// Package combination ...
package combination
import "fmt"
// Combinations structure with in and out rune
type Combinations struct {
out []rune
in []rune
}
// Start ...
func Start(input string) {
c := &Combinations{
in: []rune(input),
}
c.Combine(0)
}
// Combine ...
func (c *Combinations) Combine(seed int) {
inLen := len(c.in)
for i := seed; i < inLen-1; i++ {
c.out = append(c.out, c.in[i])
fmt.Println(string(c.out))
c.Combine(i + 1)
c.out = c.out[:len(c.out)-1]
}
c.out = append(c.out, c.in[inLen-1])
fmt.Println(string(c.out))
c.out = c.out[:len(c.out)-1]
}
================================================
FILE: strings/doc.go
================================================
// Package strings is a package that contains all algorithms
// that are used to analyse and manipulate strings.
package strings
================================================
FILE: strings/generateparentheses/generateparentheses.go
================================================
// generateparenthesesgo
// description: Generate Parentheses
// details:
// Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
// author(s) [red_byte](https://github.com/i-redbyte)
// see generateparentheses_test.go
package generateparentheses
import "strings"
func GenerateParenthesis(n int) []string {
result := make([]string, 0)
maxLen := 2 * n
var recursiveComputation func(s []string, left int, right int)
recursiveComputation = func(s []string, left int, right int) {
if len(s) == maxLen {
result = append(result, strings.Join(s, ""))
return
}
if left < n {
s = append(s, "(")
recursiveComputation(s, left+1, right)
s = s[:len(s)-1]
}
if right < left {
s = append(s, ")")
recursiveComputation(s, left, right+1)
_ = s[:len(s)-1]
}
}
recursiveComputation(make([]string, 0), 0, 0)
return result
}
================================================
FILE: strings/generateparentheses/generateparentheses_test.go
================================================
// generateparentheses_test.go
// description: Generate Parentheses
// author(s) [red_byte](https://github.com/i-redbyte)
// see generateparentheses.go
package generateparentheses
import "testing"
func TestGenerateParenthesis(t *testing.T) {
t.Run("GenerateParenthesis", func(t *testing.T) {
result := GenerateParenthesis(3)
t.Log(result)
exp := []string{"((()))", "(()())", "(())()", "()(())", "()()()"}
for i, v := range result {
if v != exp[i] {
t.Errorf("Wrong result! Expected:%s, returned:%s ", result[i], exp[i])
}
}
})
}
================================================
FILE: strings/genetic/genetic.go
================================================
// Package genetic provides functions to work with strings
// using genetic algorithm. https://en.wikipedia.org/wiki/Genetic_algorithm
//
// Author: D4rkia
package genetic
import (
"errors"
"fmt"
"math/rand"
"sort"
"strconv"
"time"
"unicode/utf8"
)
// Population item represent a single step in the evolution process.
// One can think of population item as a single species.
// Key stands for the actual data entity of the species, which is a string
// in current implementation. Key can be interpreted as species DNA.
// Value shows how close this species to the desired target, where 1 means,
// that species DNA equals to the targeted one, 0 for no matchings in the DNA.
//
// **Note** In the current implementation species DNA length is suppose to be
// equal to the target length for algorithm to work.
type PopulationItem struct {
Key string
Value float64
}
// Conf stands for configurations set provided to GeneticString function.
type Conf struct {
// Maximum size of the population.
// Bigger could be faster but more memory expensive.
PopulationNum int
// Number of elements selected in every generation for evolution
// the selection takes. Place from the best to the worst of that
// generation must be smaller than PopulationNum.
SelectionNum int
// Probability that an element of a generation can mutate changing one of
// its genes this guarantees that all genes will be used during evolution.
MutationProb float64
// Enables debugging output to the console.
Debug bool
}
// Result structure contains generation process statistics, as well as the
// best resulted population item.
type Result struct {
// Number of generations steps performed.
Generation int
// Number of generated population items.
Analyzed int
// Result of generation with the best Value.
Best PopulationItem
}
// GeneticString generates PopulationItem based on the imputed target
// string, and a set of possible runes to build a string with. In order
// to optimise string generation additional configurations can be provided
// with Conf instance. Empty instance of Conf (&Conf{}) can be provided,
// then default values would be set.
//
// Link to the same algorithm implemented in python:
// https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_string.py
func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error) {
populationNum := conf.PopulationNum
if populationNum == 0 {
populationNum = 200
}
selectionNum := conf.SelectionNum
if selectionNum == 0 {
selectionNum = 50
}
// Verify if 'populationNum' s bigger than 'selectionNum'
if populationNum < selectionNum {
return nil, errors.New("populationNum must be bigger than selectionNum")
}
mutationProb := conf.MutationProb
if mutationProb == .0 {
mutationProb = .4
}
debug := conf.Debug
// Just a seed to improve randomness required by the algorithm
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
// Verify that the target contains no genes besides the ones inside genes variable.
for position, r := range target {
invalid := true
for _, n := range charmap {
if n == r {
invalid = false
}
}
if invalid {
message := fmt.Sprintf("character not available in charmap at position: %v", position)
return nil, errors.New(message)
}
}
// Generate random starting population
pop := make([]PopulationItem, populationNum)
for i := 0; i < populationNum; i++ {
key := ""
for x := 0; x < utf8.RuneCountInString(target); x++ {
choice := rnd.Intn(len(charmap))
key += string(charmap[choice])
}
pop[i] = PopulationItem{key, 0}
}
// Just some logs to know what the algorithms is doing
gen, generatedPop := 0, 0
// This loop will end when we will find a perfect match for our target
for {
gen++
generatedPop += len(pop)
// Random population created now it's time to evaluate
for i, item := range pop {
pop[i].Value = 0
itemKey, targetRune := []rune(item.Key), []rune(target)
for x := 0; x < len(target); x++ {
if itemKey[x] == targetRune[x] {
pop[i].Value++
}
}
pop[i].Value = pop[i].Value / float64(len(targetRune))
}
sort.SliceStable(pop, func(i, j int) bool { return pop[i].Value > pop[j].Value })
// Check if there is a matching evolution
if pop[0].Key == target {
break
}
// Print the best resultPrint the Best result every 10 generations
// just to know that the algorithm is working
if debug && gen%10 == 0 {
fmt.Println("Generation:", strconv.Itoa(gen), "Analyzed:", generatedPop, "Best:", pop[0])
}
// Generate a new population vector keeping some of the best evolutions
// Keeping this avoid regression of evolution
var popChildren []PopulationItem
popChildren = append(popChildren, pop[0:int(selectionNum/3)]...)
// This is Selection
for i := 0; i < int(selectionNum); i++ {
parent1 := pop[i]
// Generate more child proportionally to the fitness score
nChild := (parent1.Value * 100) + 1
if nChild >= 10 {
nChild = 10
}
for x := 0.0; x < nChild; x++ {
parent2 := pop[rnd.Intn(selectionNum)]
// Crossover
split := rnd.Intn(utf8.RuneCountInString(target))
child1 := append([]rune(parent1.Key)[:split], []rune(parent2.Key)[split:]...)
child2 := append([]rune(parent2.Key)[:split], []rune(parent1.Key)[split:]...)
// Clean fitness value
// Mutate
if rnd.Float64() < mutationProb {
child1[rnd.Intn(len(child1))] = charmap[rnd.Intn(len(charmap))]
}
if rnd.Float64() < mutationProb {
child2[rnd.Intn(len(child2))] = charmap[rnd.Intn(len(charmap))]
}
// Push into 'popChildren'
popChildren = append(popChildren, PopulationItem{string(child1), 0})
popChildren = append(popChildren, PopulationItem{string(child2), 0})
// Check if the population has already reached the maximum value and if so,
// break the cycle. If this check is disabled the algorithm will take
// forever to compute large strings but will also calculate small string in
// a lot fewer generationsù
if len(popChildren) >= selectionNum {
break
}
}
}
pop = popChildren
}
return &Result{gen, generatedPop, pop[0]}, nil
}
================================================
FILE: strings/genetic/geneticalgorithm_test.go
================================================
package genetic
import (
"testing"
)
func TestSimple(t *testing.T) {
target := "This is a genetic algorithm to evaluate, combine, evolve and mutate a string!"
charmap := []rune(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,;!?+-*#@^'èéòà€ù=)(&%$£/\\")
res, err := GeneticString(target, charmap, &Conf{})
if err != nil {
t.Errorf("Unexpected error: %v\n", err)
}
got := res.Best
if got.Value != float64(1.0) {
t.Errorf("Target value not reached\nwant: %f\n, got: %f\n", float64(1.0), got.Value)
}
if got.Key != target {
t.Errorf("Target string not reached\nwant: %s\n, got: %s\n", target, got.Key)
}
}
================================================
FILE: strings/guid/guid.go
================================================
// guid.go
// description: Generate random globally unique identifiers (GUIDs).
// details:
// A GUID (globally unique identifier) is a 128-bit text string that
// represents an identification (ID). Organizations generate GUIDs when
// a unique reference number is needed to identify information on
// a computer or network. A GUID can be used to ID hardware, software,
// accounts, documents and other items. The term is also often used in
// software created by Microsoft.
// See more information on: https://en.wikipedia.org/wiki/Universally_unique_identifier
// author(s) [cheatsnake](https://github.com/cheatsnake)
// see guid_test.go
// Package guid provides facilities for generating random globally unique identifiers.
package guid
import (
"crypto/rand"
"fmt"
"math/big"
"strings"
)
const pattern string = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx"
const versionIndex int = 14
// New returns a randomly generated global unique identifier.
func New() (string, error) {
var guid strings.Builder
for i, ch := range pattern {
if i == versionIndex {
guid.WriteRune(ch)
continue
}
if ch == '-' {
guid.WriteRune(ch)
continue
}
random, err := rand.Int(rand.Reader, big.NewInt(16))
if err != nil {
return "", err
}
guid.WriteString(fmt.Sprintf("%x", random.Int64()))
}
return guid.String(), nil
}
================================================
FILE: strings/guid/guid_test.go
================================================
package guid
import (
"strings"
"testing"
)
func TestNew(t *testing.T) {
t.Run("check for allowed characters", func(t *testing.T) {
allowedChars := "0123456789abcdef-"
guid, err := New()
if err != nil {
t.Errorf(`the test failed, an error occurred: %s`, err.Error())
}
for _, char := range guid {
if !strings.Contains(allowedChars, string(char)) {
t.Errorf(`allowed only "%s" characters, but got %v`, allowedChars, char)
}
}
})
t.Run("check string length", func(t *testing.T) {
guid, err := New()
if err != nil {
t.Errorf(`the test failed, an error occurred: %s`, err.Error())
}
if len(guid) != len(pattern) {
t.Errorf(`the length of the string should be "%d", but got %d`, len(pattern), len(guid))
}
})
t.Run("check for version index", func(t *testing.T) {
expected := "4"
versionIndex := strings.Index(pattern, expected)
guid, err := New()
if err != nil {
t.Errorf(`the test failed, an error occurred: %s`, err.Error())
}
result := string(guid[versionIndex])
if expected != result {
t.Errorf(`at the index %d should be %s, but got %s`, versionIndex, expected, result)
}
})
t.Run("check the number of dashes", func(t *testing.T) {
expected := strings.Count(pattern, "-")
guid, err := New()
if err != nil {
t.Errorf(`the test failed, an error occurred: %s`, err.Error())
}
result := strings.Count(guid, "-")
if expected != result {
t.Errorf(`the length of the string should be "%d", but got %d`, len(pattern), len(guid))
}
})
}
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = New()
}
}
================================================
FILE: strings/hamming/hammingdistance.go
================================================
/*
This algorithm calculates the hamming distance between two equal length strings.
The Hamming distance between two equal-length strings of symbols is the number of positions
at which the corresponding symbols are different:
https://en.wikipedia.org/wiki/Hamming_distance
Note that we didn't consider strings as an array of bytes, therefore, we didn't use the XOR operator.
In this case, we used a simple loop to compare each character of the strings, and if they are different,
we increment the hamming distance by 1.
Parameters: two strings to compare
Output: distance between both strings */
package hamming
import "errors"
func Distance(str1, str2 string) (int, error) {
if len(str1) != len(str2) {
return -1, errors.New("strings must have a same length")
}
hammingDistance := 0
for i := 0; i < len(str1); i++ {
if str1[i] != str2[i] {
hammingDistance++
}
}
return hammingDistance, nil
}
================================================
FILE: strings/hamming/hammingdistance_test.go
================================================
package hamming
import "testing"
var testCases = []struct {
name string
string1 string
string2 string
expected int
}{
{
"empty strings",
"",
"",
0,
},
{
"single character strings",
"A",
"A",
0,
},
{
"two different strings with a same length",
"TestString 1",
"TestString 2",
1,
},
{
"two different strings with a different length",
"TestString1",
"TestString",
-1,
},
{
"two same strings with a same length",
"TestString",
"TestString",
0,
},
}
func TestHammingDistance(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual, err := Distance(tc.string1, tc.string2)
if err != nil {
if tc.expected != -1 {
t.Fatalf("Expected no error, but got %v", err)
}
} else if actual != tc.expected {
t.Errorf("Expected Hamming distance between strings: '%s' and '%s' is %v, but got: %v", tc.string1, tc.string2, tc.expected, actual)
}
})
}
}
================================================
FILE: strings/horspool/horspool.go
================================================
// Implementation of the
// [Boyer–Moore–Horspool algorithm](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm)
package horspool
import "errors"
var ErrNotFound = errors.New("pattern was not found in the input string")
func Horspool(t, p string) (int, error) {
// in order to handle multy-byte character properly
// the input is converted into rune arrays
return horspool([]rune(t), []rune(p))
}
func horspool(t, p []rune) (int, error) {
shiftMap := computeShiftMap(t, p)
pos := 0
for pos <= len(t)-len(p) {
if isMatch(pos, t, p) {
return pos, nil
}
if pos+len(p) >= len(t) {
// because the remaining length of the input string
// is the same as the length of the pattern
// and it does not match the pattern
// it is impossible to find the pattern
break
}
// because of the check above
// t[pos+len(p)] is defined
pos += shiftMap[t[pos+len(p)]]
}
return -1, ErrNotFound
}
// Checks if the array p matches the subarray of t starting at pos.
// Note that backward iteration.
// There are [other](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm#Tuning_the_comparison_loop)
// approaches possible.
func isMatch(pos int, t, p []rune) bool {
j := len(p)
for j > 0 && t[pos+j-1] == p[j-1] {
j--
}
return j == 0
}
func computeShiftMap(t, p []rune) (res map[rune]int) {
res = make(map[rune]int)
for _, tCode := range t {
res[tCode] = len(p)
}
for i, pCode := range p {
res[pCode] = len(p) - i
}
return res
}
================================================
FILE: strings/horspool/horspool_test.go
================================================
// horspool_test.go
// description: Tests for horspool
// see horspool.go
package horspool
import "testing"
import "fmt"
func TestLHorspool(t *testing.T) {
testCases := []struct {
input string
pattern string
expected int
}{
{"aaaaXaaa", "X", 4},
{"aaaaXXaa", "XX", 4},
{"Xaaab", "X", 0},
{"XYaab", "XY", 0},
{"abcefghXYZ", "XYZ", 7},
{"abcefgh€YZ⌘", "€YZ", 7},
{"⌘bcefgh€YZ⌘", "€YZ", 7},
{"abc", "abc", 0},
{"", "", 0},
{"a", "", 0},
{"a", "a", 0},
{"aa", "a", 0},
{"aa", "aa", 0},
}
for _, tc := range testCases {
t.Run(fmt.Sprint("test with ", tc.input, " ", tc.pattern), func(t *testing.T) {
result, curError := Horspool(tc.input, tc.pattern)
if curError != nil {
t.Fatalf("Got unexpected error")
}
if tc.expected != result {
t.Fatalf("expected %d, got %d", tc.expected, result)
}
})
}
}
func TestLHorspoolNotExisintPattern(t *testing.T) {
testCases := []struct {
input string
pattern string
}{
{"", "X"},
{"X", "Y"},
{"X", "XX"},
{"aaaaaaaXaXaaaa", "XXX"},
{"aaaaaaaXaX", "XXX"},
{"XaX", "XXX"},
{"XaX", "XXX"},
{"\xe2\x8c\x98", "\x98"},
}
for _, tc := range testCases {
t.Run(fmt.Sprint("test with ", tc.input, " ", tc.pattern), func(t *testing.T) {
result, curError := Horspool(tc.input, tc.pattern)
if curError != ErrNotFound {
t.Fatalf("Got unexpected error")
}
if result != -1 {
t.Fatalf("expected -1, got %d", result)
}
})
}
}
================================================
FILE: strings/isisogram.go
================================================
// Checks if a given string is an isogram.
// A first-order isogram is a word in which no letter of the alphabet occurs more than once.
// A second-order isogram is a word in which each letter appears twice.
// A third-order isogram is a word in which each letter appears three times.
// wiki: https://en.wikipedia.org/wiki/Heterogram_(literature)#Isograms
// Author: M3talM0nk3y
package strings
import (
"errors"
"regexp"
"strings"
)
type IsogramOrder int
const (
First IsogramOrder = iota + 1
Second
Third
)
func hasDigit(text string) bool {
re := regexp.MustCompile(`\d`)
return re.MatchString(text)
}
func hasSymbol(text string) bool {
re := regexp.MustCompile(`[-!@#$%^&*()+]`)
return re.MatchString(text)
}
func IsIsogram(text string, order IsogramOrder) (bool, error) {
if order < First || order > Third {
return false, errors.New("Invalid isogram order provided")
}
text = strings.ToLower(text)
text = strings.Join(strings.Fields(text), "")
if hasDigit(text) || hasSymbol(text) {
return false, errors.New("Cannot contain numbers or symbols")
}
letters := make(map[string]int)
for _, c := range text {
l := string(c)
if _, ok := letters[l]; ok {
letters[l] += 1
if letters[l] > 3 {
return false, nil
}
continue
}
letters[l] = 1
}
mapVals := make(map[int]bool)
for _, v := range letters {
mapVals[v] = true
}
if _, ok := mapVals[int(order)]; ok && len(mapVals) == 1 {
return true, nil
}
return false, nil
}
================================================
FILE: strings/isisogram_test.go
================================================
package strings_test
import (
"errors"
"testing"
"github.com/TheAlgorithms/Go/strings"
)
var testCases = []struct {
name string
input string
order strings.IsogramOrder
expectedVal bool
expectedErr error
}{
{
"Alphanumeric string 1",
"copy1",
1,
false,
errors.New("Cannot contain numbers or symbols"),
},
{
"Alphanumeric string 2",
"copy1sentence",
1,
false,
errors.New("Cannot contain numbers or symbols"),
},
{
"Alphanumeric string 3",
"copy1 sentence with space",
1,
false,
errors.New("Cannot contain numbers or symbols"),
},
{
"Alphabetic string 1",
"allowance",
1,
false,
nil,
},
{
"Alphabetic string 2",
"My Doodle",
1,
false,
nil,
},
{
"Alphabetic string with symbol",
"Isogram!",
1,
false,
errors.New("Cannot contain numbers or symbols"),
},
{
"Isogram string 1",
"Uncopyrightable",
1,
true,
nil,
},
{
"Second order isogram 1",
"Caucasus",
2,
true,
nil,
},
{
"Second order isogram 2",
"Couscous",
2,
true,
nil,
},
{
"Third order isogram 1",
"Deeded",
3,
true,
nil,
},
{
"Third order isogram 2",
"Sestettes",
3,
true,
nil,
},
{
"Not an isogram",
"Pneumonoultramicroscopicsilicovolcanoconiosis",
1,
false,
nil,
},
{
"Not an isogram",
"Randomstring",
4,
false,
errors.New("Invalid isogram order provided"),
},
{
"Third order isogram checked as first order",
"Deeded",
1,
false,
nil,
},
}
func TestIsIsogram(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
actualVal, actualErr := strings.IsIsogram(test.input, test.order)
if actualErr != nil && test.expectedErr.Error() != actualErr.Error() {
t.Errorf("Expected to be '%v' for string %s but was '%v'.", test.expectedErr, test.input, actualErr)
}
if test.expectedVal != actualVal {
t.Errorf("Expected to be '%v' for string %s but was '%v'.", test.expectedVal, test.input, actualVal)
}
})
}
}
================================================
FILE: strings/issubsequence.go
================================================
// Checks if a given string is a subsequence of another string.
// A subsequence of a given string is a string that can be derived from the given
// string by deleting some or no characters without changing the order of the
// remaining characters. (i.e., "dpr" is a subsequence of "depqr" while "drp" is not).
// Author: sanjibgirics
package strings
// Returns true if s is subsequence of t, otherwise return false.
func IsSubsequence(s string, t string) bool {
if len(s) > len(t) {
return false
}
if s == t {
return true
}
if len(s) == 0 {
return true
}
sIndex := 0
for tIndex := 0; tIndex < len(t); tIndex++ {
if s[sIndex] == t[tIndex] {
sIndex++
}
if sIndex == len(s) {
return true
}
}
return false
}
================================================
FILE: strings/issubsequence_test.go
================================================
package strings_test
import (
"reflect"
"testing"
"github.com/TheAlgorithms/Go/strings"
)
func TestIsSubsequence(t *testing.T) {
var testCases = []struct {
name string
s string
t string
expected bool
}{
{
"Valid case 1 ",
"ace",
"abcde",
true,
},
{
"Invalid case 1",
"aec",
"abcde",
false,
},
{
"Empty strings",
"",
"",
true,
},
{
"s is more then t",
"aeccccc",
"abcde",
false,
},
{
"s is empty",
"",
"abcde",
true,
},
{
"Equal strings",
"aec",
"aec",
true,
},
{
"Valid case 2",
"pyr",
"wpxqyrz",
true,
},
{
"Invalid case 2",
"prx",
"wpxqyrz",
false,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
funcResult := strings.IsSubsequence(test.s, test.t)
if !reflect.DeepEqual(test.expected, funcResult) {
t.Errorf("expected: %v, got %v", test.expected, funcResult)
}
})
}
}
================================================
FILE: strings/kmp/kmp.go
================================================
package kmp
// Kmp Function kmp performing the Knuth-Morris-Pratt algorithm.
func Kmp(word, text string, patternTable []int) []int {
if len(word) > len(text) {
return nil
}
var (
i, j int
matches []int
)
for i+j < len(text) {
if word[j] == text[i+j] {
j++
if j == len(word) {
matches = append(matches, i)
i = i + j
j = 0
}
} else {
i = i + j - patternTable[j]
if patternTable[j] > -1 {
j = patternTable[j]
} else {
j = 0
}
}
}
return matches
}
// table building for kmp algorithm.
func table(w string) []int {
var (
t []int = []int{-1}
k int
)
for j := 1; j < len(w); j++ {
k = j - 1
for w[0:k] != w[j-k:j] && k > 0 {
k--
}
t = append(t, k)
}
return t
}
================================================
FILE: strings/kmp/kmp_test.go
================================================
package kmp
import (
"reflect"
"testing"
)
func TestKmp(t *testing.T) {
type args struct {
word string
text string
patternTable []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "test1",
args: args{
word: "ab",
text: "ababacaab",
patternTable: table("ababacaab"),
},
want: []int{0, 2, 7},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Kmp(tt.args.word, tt.args.text, tt.args.patternTable); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Kmp() = %v, want %v", got, tt.want)
}
})
}
}
func TestTable(t *testing.T) {
type args struct {
w string
}
tests := []struct {
name string
args args
want []int
}{
{
name: "test1",
args: args{
w: "ababacaab",
},
want: []int{-1, 0, 0, 1, 2, 3, 0, 1, 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := table(tt.args.w); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Table() = %v, want %v", got, tt.want)
}
})
}
}
================================================
FILE: strings/levenshtein/levenshteindistance.go
================================================
/*
This algorithm calculates the distance between two strings.
Parameters: two strings to compare and weights of insertion, substitution and deletion.
Output: distance between both strings
*/
package levenshtein
// Distance Function that gives Levenshtein Distance
func Distance(str1, str2 string, icost, scost, dcost int) int {
row1 := make([]int, len(str2)+1)
row2 := make([]int, len(str2)+1)
for i := 1; i <= len(str2); i++ {
row1[i] = i * icost
}
for i := 1; i <= len(str1); i++ {
row2[0] = i * dcost
for j := 1; j <= len(str2); j++ {
if str1[i-1] == str2[j-1] {
row2[j] = row1[j-1]
} else {
ins := row2[j-1] + icost
del := row1[j] + dcost
sub := row1[j-1] + scost
if ins < del && ins < sub {
row2[j] = ins
} else if del < sub {
row2[j] = del
} else {
row2[j] = sub
}
}
}
row1, row2 = row2, row1
}
return row1[len(row1)-1]
}
================================================
FILE: strings/levenshtein/levenshteindistance_test.go
================================================
package levenshtein
import "testing"
var testCases = []struct {
name string
string1 string
string2 string
insertionCost int
substitutionCost int
deletionCost int
expected int
}{
{
"strings with equal operation weights.",
"stingy",
"ring",
1,
1,
1,
3,
},
{
"strings with different operation weights.",
"stingy",
"ring",
1,
1,
3,
7,
},
{
"strings with different operation weights.",
"kitten",
"sitting",
1,
1,
1,
3,
},
}
func TestLevenshteinDistance(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := Distance(tc.string1, tc.string2, tc.insertionCost, tc.substitutionCost, tc.deletionCost)
if actual != tc.expected {
t.Errorf("Expected Levenshtein distance between strings: '%s' and '%s' is %v, but got: %v", tc.string1, tc.string2, tc.expected, actual)
}
})
}
}
================================================
FILE: strings/manacher/longestpalindrome.go
================================================
// longestpalindrome.go
// description: Manacher's algorithm (Longest palindromic substring)
// details:
// An algorithm with linear running time that allows you to get compressed information about all palindromic substrings of a given string. - [Manacher's algorithm](https://en.wikipedia.org/wiki/Longest_palindromic_substring)
// author(s) [red_byte](https://github.com/i-redbyte)
// see longestpalindrome_test.go
package manacher
import (
"github.com/TheAlgorithms/Go/math/min"
"strings"
)
func makeBoundaries(s string) string {
var result strings.Builder
result.WriteRune('#')
for _, ch := range s {
if ch != ' ' { //ignore space as palindrome character
result.WriteRune(ch)
}
result.WriteRune('#')
}
return result.String()
}
func nextBoundary(s string) string {
var result strings.Builder
for _, ch := range s {
if ch != '#' {
result.WriteRune(ch)
}
}
return result.String()
}
func LongestPalindrome(s string) string {
boundaries := makeBoundaries(s)
b := make([]int, len(boundaries))
k := 0
index := 0
maxLen := 0
maxCenterSize := 0
for i := range b {
if i < k {
b[i] = min.Int(b[2*index-i], k-i)
} else {
b[i] = 1
}
for i-b[i] >= 0 && i+b[i] < len(boundaries) && boundaries[i-b[i]] == boundaries[i+b[i]] {
b[i] += 1
}
if maxLen < b[i]-1 {
maxLen = b[i] - 1
maxCenterSize = i
}
if b[i]+i-1 > k {
k = b[i] + i - 1
index = i
}
}
return nextBoundary(boundaries[maxCenterSize-maxLen : maxCenterSize+maxLen])
}
================================================
FILE: strings/manacher/longestpalindrome_test.go
================================================
// longestpalindrome_test.go
// description: Manacher's algorithm (Longest palindromic substring)
// author(s) [red_byte](https://github.com/i-redbyte)
// see longestpalindrome.go
package manacher
import "testing"
func getTests() []struct {
s string
result string
} {
var tests = []struct {
s string
result string
}{
{"olokazakabba", "kazak"},
{"abaacakkkkk", "kkkkk"},
{"qqqq C++ groovy mom pooop", "pooop"},
{"CCCPCCC @@@ hello", "CCCPCCC"},
{"goog gogogogogog -go- gogogogo ", "gogogogogog"},
}
return tests
}
func TestLongestPalindrome(t *testing.T) {
tests := getTests()
for _, tv := range tests {
t.Run(tv.s, func(t *testing.T) {
result := LongestPalindrome(tv.s)
t.Log(tv.s, " ", result)
if result != tv.result {
t.Errorf("Wrong result! Expected:%s, returned:%s ", tv.result, result)
}
})
}
}
================================================
FILE: strings/palindrome/ispalindrome.go
================================================
// ispalindrome.go
// description: Checks if a given string is palindrome or not
// details:
// Palindromes are expressions that read the same way forwards and backwards.
// They can be words/phrases (like "racecar" and "Do geese see God?"), or even
// numbers (like "02/02/2020"). Usually punctuation signs, capitalization
// and spaces are ignored. A regular expression was used to achieve that.
// See more information on: https://en.wikipedia.org/wiki/Palindrome
// author(s) [Fernanda Kawasaki](https://github.com/fernandakawasaki)
// see ispalindrome_test.go
package palindrome
import (
"regexp"
"strings"
)
func cleanString(text string) string {
clean_text := strings.ToLower(text)
clean_text = strings.Join(strings.Fields(clean_text), "") // Remove spaces
regex, _ := regexp.Compile(`[^\p{L}\p{N} ]+`) // Regular expression for alphanumeric only characters
return regex.ReplaceAllString(clean_text, "")
}
func IsPalindrome(text string) bool {
clean_text := cleanString(text)
var i, j int
rune := []rune(clean_text)
for i = 0; i < len(rune)/2; i++ {
j = len(rune) - 1 - i
if string(rune[i]) != string(rune[j]) {
return false
}
}
return true
}
func IsPalindromeRecursive(text string) bool {
clean_text := cleanString(text)
runes := []rune(clean_text)
return isPalindromeRecursiveHelper(runes, 0, int64(len(runes)))
}
func isPalindromeRecursiveHelper(runes []rune, start int64, end int64) bool {
if start >= end {
return true
}
if runes[start] != runes[end-1] {
return false
}
start = start + 1
end = end - 1
return isPalindromeRecursiveHelper(runes, start, end)
}
================================================
FILE: strings/palindrome/ispalindrome_test.go
================================================
package palindrome
import (
"testing"
)
var testCases = []struct {
name string // test description
input string // user input
expected bool // expected return
}{
{
"non palindrome string",
"According to the laws of aviation bees can't flyã",
false,
},
{
"non palindrome string",
"Alô?",
false,
},
{
"palindrome string 1",
"Do geese see God?",
true,
},
{
"palindrome string 2",
"ΝΙΨΟΝ ΑΝΟΜΗΜΑΤΑ ΜΗ ΜΟΝΑΝ ΟΨΙΝ",
true,
},
{
"palindrome string 3",
"Was it a car or a cat I saw?",
true,
},
{
"empty string",
"",
true,
},
}
func TestIsPalindrome(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
func_result := IsPalindrome(test.input)
if test.expected != func_result {
t.Errorf("Expected answer '%t' for string '%s' but answer given was %t", test.expected, test.input, func_result)
}
})
}
}
func TestIsPalindromeRecursive(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
func_result := IsPalindromeRecursive(test.input)
if test.expected != func_result {
t.Errorf("Expected answer '%t' for string '%s' but answer given was %t", test.expected, test.input, func_result)
}
})
}
}
================================================
FILE: strings/pangram/ispangram.go
================================================
// ispangram.go
// description: Checks if a given string is pangram or not
// details: A pangram is a sentence or expression that uses all the letters of the alphabet.
// Reference: https://www.geeksforgeeks.org/pangram-checking/
// Author : Kavitha J
package pangram
import (
"regexp"
"strings"
)
func cleanString(text string) string {
cleanText := strings.ToLower(text) // Convert to lowercase
cleanText = strings.Join(strings.Fields(cleanText), "") // Remove spaces
regex, _ := regexp.Compile(`[^\p{L}\p{N} ]+`) // Regular expression for alphanumeric only characters
return regex.ReplaceAllString(cleanText, "")
}
func IsPangram(text string) bool {
cleanText := cleanString(text)
if len(cleanText) < 26 {
return false
}
var data = make(map[rune]bool)
for _, i := range cleanText {
data[i] = true
}
return len(data) == 26
}
================================================
FILE: strings/pangram/ispangram_test.go
================================================
package pangram
import (
"testing"
)
var testCases = []struct {
name string // test description
input string // user input
expected bool // expected return
}{
{
"empty string",
"",
false,
},
{
"non pangram string without spaces",
"abc",
false,
},
{
"non pangram string with spaces",
"Hello World",
false,
},
{
"Pangram string 1",
" Abcdefghijklmnopqrstuvwxyz",
true,
},
{
"pangram string 2",
"cdefghijklmnopqrstuvwxABC zyb",
true,
},
{
"pangram string 3",
"The Quick Brown Fox Jumps Over the Lazy Dog",
true,
},
}
func TestIsPangram(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
func_result := IsPangram(test.input)
if test.expected != func_result {
t.Errorf("Expected answer '%t' for string '%s' but answer given was %t", test.expected, test.input, func_result)
}
})
}
}
================================================
FILE: strings/parenthesis/parenthesis.go
================================================
package parenthesis
// Parenthesis algorithm checks if every opened parenthesis
// is closed correctly. When parcounter is less than 0 when a closing
// parenthesis is detected without an opening parenthesis
// that surrounds it and parcounter will be 0 if all open
// parenthesis are closed correctly.
func Parenthesis(text string) bool {
parcounter := 0
for _, r := range text {
switch r {
case '(':
parcounter++
case ')':
parcounter--
}
if parcounter < 0 {
return false
}
}
return parcounter == 0
}
================================================
FILE: strings/parenthesis/parenthesis_test.go
================================================
package parenthesis
import (
"testing"
)
var parenthesisTestCases = []struct {
name string
text string
expected bool
}{
{
"simple test with one level deep",
"(3*9-2)+(3/7^3)",
true,
},
{
"three nested parenthesis with three deep level",
"(-1*(5+2^(3-4)*(7.44-12)+6.66/(3.43-(1+2)))*(sqrt(3-4)))",
true,
},
{
"one opened parenthesis without be closed",
"(2*9-17)*((7+3/3)*2*(-1+4)",
false,
},
{
"one open parenthesis for each close one but not in pairs",
"(4*(39.22-7.4)/6.77))(",
false,
},
{
"6 deep level",
"(5+33.2)*((8.04-6.5)*(1 - (1^(2-1.22*5.44+(4.33*7.2^(0.34*(-1.23+2)))))))",
true,
},
{
"inverted parenthesis",
")()()()()(",
false,
},
}
func TestParenthesis(t *testing.T) {
for _, tc := range parenthesisTestCases {
t.Run(tc.name, func(t *testing.T) {
actual := Parenthesis(tc.text)
if actual != tc.expected {
t.Errorf("Expected %t value from test %s with input %s\n", tc.expected, tc.name, tc.text)
}
})
}
}
================================================
FILE: strings/search/boyermoore.go
================================================
package search
// Implementation of boyer moore string search
// O(l) where l=len(text)
func BoyerMoore(text string, pattern string) []int {
var positions []int
l := len(text)
n := len(pattern)
// using booyer moore horspool modification
// O(n) space instead of O(n**2)
bcr := make(map[byte]int)
for i := 0; i < n-1; i++ {
bcr[pattern[i]] = n - i - 1
}
// Apostolico–Giancarlo modification
// allow to skip patterns that we know matches
// let us do O(l) instead of O(ln)
skips := make(map[int]int)
for _, s := range bcr {
i := 0
for ; i < n-s; i++ {
if pattern[n-1-i] != pattern[n-1-s-i] {
break
}
}
skips[s] = i
}
skip := 0
jump := n
for i := 0; i < l-n+1; {
skip = skips[jump]
for k := n - 1; k > -1; k-- {
if text[i+k] != pattern[k] {
jump, ok := bcr[text[i+k]]
if !ok {
jump = n
}
i += jump
break
}
if k == n-jump {
k -= skip
}
if k == 0 {
positions = append(positions, i)
jump = 1
i += jump
}
}
}
return positions
}
================================================
FILE: strings/search/naive.go
================================================
package search
// Implementation of naive string search
// O(n*m) where n=len(txt) and m=len(pattern)
func Naive(text string, pattern string) []int {
var positions []int
for i := 0; i <= len(text)-len(pattern); i++ {
var match bool = true
for j := 0; j < len(pattern); j++ {
if text[i+j] != pattern[j] {
match = false
break
}
}
if match {
positions = append(positions, i)
}
}
return positions
}
================================================
FILE: strings/search/patternsearch_test.go
================================================
package search
import (
"reflect"
"testing"
)
var testCases = []struct {
name string
input string
pattern string
expected []int
}{
{
"string with multiple pattern matches",
"ABAAABCDBBABCDDEBCABC",
"ABC",
[]int{4, 10, 18},
},
{
"string with single pattern match",
"ABCDEFGHIJKLMN",
"CDE",
[]int{2},
},
{
"string with no pattern match",
"ABCDEFGHIJKLMN",
"XYZ",
[]int(nil),
},
{
"empty string",
"",
"XYZ",
[]int(nil),
},
{
"worse case 1",
"AAAAAAAAAA",
"AAA",
[]int{0, 1, 2, 3, 4, 5, 6, 7},
},
{
"worse case 2",
"NANANANANANANANANA",
"NANANA",
[]int{0, 2, 4, 6, 8, 10, 12},
},
}
func TestNaive(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := Naive(tc.input, tc.pattern)
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("Expected matches for pattern '%s' for string '%s' are: %v, but actual matches are: %v", tc.pattern, tc.input, tc.expected, actual)
}
})
}
}
func TestBooyerMoore(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := BoyerMoore(tc.input, tc.pattern)
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("Expected matches for pattern '%s' for string '%s' are: %v, but actual matches are: %v", tc.pattern, tc.input, tc.expected, actual)
}
})
}
}
================================================
FILE: strings/strings_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package strings
================================================
FILE: structure/circularqueue/circularqueue_test.go
================================================
package circularqueue
import "testing"
func TestCircularQueue(t *testing.T) {
t.Run("Size Check", func(t *testing.T) {
_, err := NewCircularQueue[int](-3)
if err == nil {
t.Errorf("Expected error, got nil")
}
queue, _ := NewCircularQueue[int](5)
expectedSize := 5
gotSize := queue.Size()
if gotSize != expectedSize {
t.Errorf("Expected size: %v, got: %v\n", expectedSize, gotSize)
}
if err := queue.Enqueue(1); err != nil {
t.Error(err)
}
if err := queue.Enqueue(2); err != nil {
t.Error(err)
}
if err := queue.Enqueue(3); err != nil {
t.Error(err)
}
if err := queue.Enqueue(4); err != nil {
t.Error(err)
}
if err := queue.Enqueue(5); err != nil {
t.Error(err)
}
err = queue.Enqueue(6)
if err == nil {
t.Errorf("Expected error, got nil")
}
expectedSize = 5
gotSize = queue.Size()
if gotSize != expectedSize {
t.Errorf("Expected size: %v, got: %v\n", expectedSize, gotSize)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
err = queue.Enqueue(6)
if err != nil {
t.Errorf("Expected nil, got error: %v\n", err.Error())
}
expectedSize = 5
gotSize = queue.Size()
if gotSize != expectedSize {
t.Errorf("Expected size: %v, got: %v\n", expectedSize, gotSize)
}
})
t.Run("Enqueue", func(t *testing.T) {
queue, _ := NewCircularQueue[int](10)
if err := queue.Enqueue(1); err != nil {
t.Error(err)
}
if err := queue.Enqueue(2); err != nil {
t.Error(err)
}
if err := queue.Enqueue(3); err != nil {
t.Error(err)
}
expected := 1
got, err := queue.Peek()
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
})
t.Run("Dequeue", func(t *testing.T) {
queue, _ := NewCircularQueue[string](10)
if err := queue.Enqueue("one"); err != nil {
t.Error(err)
}
if err := queue.Enqueue("two"); err != nil {
t.Error(err)
}
if err := queue.Enqueue("three"); err != nil {
t.Error(err)
}
expected := "one"
got, err := queue.Dequeue()
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
expected = "two"
got, err = queue.Peek()
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
})
t.Run("Circularity", func(t *testing.T) {
queue, _ := NewCircularQueue[int](10)
if err := queue.Enqueue(1); err != nil {
t.Error(err)
}
if err := queue.Enqueue(2); err != nil {
t.Error(err)
}
if err := queue.Enqueue(3); err != nil {
t.Error(err)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
if err := queue.Enqueue(4); err != nil {
t.Error(err)
}
if err := queue.Enqueue(5); err != nil {
t.Error(err)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
expected := 4
got, err := queue.Peek()
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
})
t.Run("IsFull", func(t *testing.T) {
queue, _ := NewCircularQueue[bool](2)
if err := queue.Enqueue(false); err != nil {
t.Error(err)
}
if err := queue.Enqueue(true); err != nil {
t.Error(err)
}
expected := true
got := queue.IsFull()
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
if _, err := queue.Dequeue(); err != nil {
t.Error(err)
}
expected = false
got = queue.IsFull()
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
})
t.Run("IsEmpty", func(t *testing.T) {
queue, _ := NewCircularQueue[float64](2)
expected := true
got := queue.IsEmpty()
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
if err := queue.Enqueue(1.0); err != nil {
t.Error(err)
}
expected = false
got = queue.IsEmpty()
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
})
t.Run("Peak", func(t *testing.T) {
queue, _ := NewCircularQueue[rune](10)
if err := queue.Enqueue('a'); err != nil {
t.Error(err)
}
if err := queue.Enqueue('b'); err != nil {
t.Error(err)
}
if err := queue.Enqueue('c'); err != nil {
t.Error(err)
}
expected := 'a'
got, err := queue.Peek()
if err != nil {
t.Error(err.Error())
}
if got != expected {
t.Errorf("Expected: %v got: %v\n", expected, got)
}
})
}
// BenchmarkCircularQueue benchmarks the CircularQueue implementation.
func BenchmarkCircularQueue(b *testing.B) {
b.Run("Enqueue", func(b *testing.B) {
queue, _ := NewCircularQueue[int](1000)
for i := 0; i < b.N; i++ {
if err := queue.Enqueue(i); err != nil {
b.Error(err)
}
}
})
b.Run("Dequeue", func(b *testing.B) {
queue, _ := NewCircularQueue[int](1000)
for i := 0; i < 1000; i++ {
if err := queue.Enqueue(i); err != nil {
b.Error(err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := queue.Dequeue(); err != nil {
b.Error(err)
}
}
})
b.Run("Peek", func(b *testing.B) {
queue, _ := NewCircularQueue[int](1000)
for i := 0; i < 1000; i++ {
if err := queue.Enqueue(i); err != nil {
b.Error(err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := queue.Peek(); err != nil {
b.Error(err)
}
}
})
}
================================================
FILE: structure/circularqueue/circularqueuearray.go
================================================
// circularqueuearray.go
// description: Implementation of a circular queue data structure
// details:
// This file contains the implementation of a circular queue data structure
// using generics in Go. The circular queue supports basic operations such as
// enqueue, dequeue, peek, and checks for full and empty states.
// author(s): [Aram Ceballos](https://github.com/aramceballos)
// ref: https://www.programiz.com/dsa/circular-queue
// ref: https://en.wikipedia.org/wiki/Circular_buffer
// Package queue provides an implementation of a circular queue data structure.
package circularqueue
// errors package: Provides functions to create and manipulate error values
import (
"errors"
)
// CircularQueue represents a circular queue data structure.
type CircularQueue[T any] struct {
items []T
front int
rear int
size int
}
// NewCircularQueue creates a new CircularQueue with the given size.
// Returns an error if the size is less than or equal to 0.
func NewCircularQueue[T any](size int) (*CircularQueue[T], error) {
if size <= 0 {
return nil, errors.New("size must be greater than 0")
}
return &CircularQueue[T]{
items: make([]T, size),
front: -1,
rear: -1,
size: size,
}, nil
}
// Enqueue adds an item to the rear of the queue.
// Returns an error if the queue is full.
func (cq *CircularQueue[T]) Enqueue(item T) error {
if cq.IsFull() {
return errors.New("queue is full")
}
if cq.IsEmpty() {
cq.front = 0
}
cq.rear = (cq.rear + 1) % cq.size
cq.items[cq.rear] = item
return nil
}
// Dequeue removes and returns the item from the front of the queue.
// Returns an error if the queue is empty.
func (cq *CircularQueue[T]) Dequeue() (T, error) {
if cq.IsEmpty() {
var zeroValue T
return zeroValue, errors.New("queue is empty")
}
retVal := cq.items[cq.front]
if cq.front == cq.rear {
cq.front = -1
cq.rear = -1
} else {
cq.front = (cq.front + 1) % cq.size
}
return retVal, nil
}
// IsFull checks if the queue is full.
func (cq *CircularQueue[T]) IsFull() bool {
return (cq.front == 0 && cq.rear == cq.size-1) || cq.front == cq.rear+1
}
// IsEmpty checks if the queue is empty.
func (cq *CircularQueue[T]) IsEmpty() bool {
return cq.front == -1 && cq.rear == -1
}
// Peek returns the item at the front of the queue without removing it.
// Returns an error if the queue is empty.
func (cq *CircularQueue[T]) Peek() (T, error) {
if cq.IsEmpty() {
var zeroValue T
return zeroValue, errors.New("queue is empty")
}
return cq.items[cq.front], nil
}
// Size returns the size of the queue.
func (cq *CircularQueue[T]) Size() int {
return cq.size
}
================================================
FILE: structure/deque/deque.go
================================================
// description: Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.
// References:
// Wikipedia : https://en.wikipedia.org/wiki/Double-ended_queue
// Github: https://www.geeksforgeeks.org/deque-set-1-introduction-applications/
// author [Sayan](https://github.com/bose-sayan)
// Package deque implements a Double Ended Queue data structure.
package deque
import (
"errors"
)
// ErrEmptyDequeue is a custom error for handling cases when some dequeuing operation is performed on an empty deque.
var ErrEmptyDequeue = errors.New("DoublyEnded queue is empty, so can't perform this operation")
type DoublyEndedQueue[T any] struct {
deque []T
}
// New returns a new DoublyEndedQueue.
func New[T any]() *DoublyEndedQueue[T] {
return &DoublyEndedQueue[T]{deque: make([]T, 0)}
}
// EnqueueFront adds an item at the front of Deque.
func (dq *DoublyEndedQueue[T]) EnqueueFront(item T) {
dq.deque = append([]T{item}, dq.deque...)
}
// EnqueueRear adds an item at the rear of Deque.
func (dq *DoublyEndedQueue[T]) EnqueueRear(item T) {
dq.deque = append(dq.deque, item)
}
// DequeueFront deletes an item from front of Deque and returns it.
func (dq *DoublyEndedQueue[T]) DequeueFront() (T, error) {
if len(dq.deque) == 0 {
var zeroVal T
return zeroVal, ErrEmptyDequeue
}
frontElement := dq.deque[0]
dq.deque = dq.deque[1:]
return frontElement, nil
}
// DequeueRear deletes an item from rear of Deque and returns it.
func (dq *DoublyEndedQueue[T]) DequeueRear() (T, error) {
if len(dq.deque) == 0 {
var zeroVal T
return zeroVal, ErrEmptyDequeue
}
rearElement := dq.deque[len(dq.deque)-1]
dq.deque = dq.deque[:len(dq.deque)-1]
return rearElement, nil
}
// Front gets the front item from queue.
func (dq *DoublyEndedQueue[T]) Front() (T, error) {
if (len(dq.deque)) == 0 {
var zeroVal T
return zeroVal, ErrEmptyDequeue
}
return dq.deque[0], nil
}
// Rear gets the last item from queue.
func (dq *DoublyEndedQueue[T]) Rear() (T, error) {
if (len(dq.deque)) == 0 {
var zeroVal T
return zeroVal, ErrEmptyDequeue
}
return dq.deque[len(dq.deque)-1], nil
}
// IsEmpty checks whether Deque is empty or not.
func (dq *DoublyEndedQueue[T]) IsEmpty() bool {
return len(dq.deque) == 0
}
// Length gets the length of Deque.
func (dq *DoublyEndedQueue[T]) Length() int {
return len(dq.deque)
}
================================================
FILE: structure/deque/deque_test.go
================================================
//
// This file contains unit tests for the deque package.
// The tests cover the following scenarios:
// - Empty deque
// - Deque with one element
// - Deque with multiple elements
// The tests are parameterized with int and string types.
// Each test case is defined with a description and a list of queries to be executed on the deque.
// The expected results and errors are also defined for each query.
//
package deque_test
import (
"testing"
"github.com/TheAlgorithms/Go/structure/deque"
)
type QueryStructure[T any] struct {
queryType string
parameter T
expectedResult interface{}
expectedError error
}
type TestCaseData[T any] struct {
description string
queries []QueryStructure[T]
}
func TestDeque(t *testing.T) {
// Test cases with ints as params
testCasesInt := []TestCaseData[int]{
{
description: "Test empty deque",
queries: []QueryStructure[int]{
{
queryType: "IsEmpty",
expectedResult: true,
expectedError: nil,
},
{
queryType: "Front",
expectedError: deque.ErrEmptyDequeue,
},
{
queryType: "Rear",
expectedError: deque.ErrEmptyDequeue,
},
{
queryType: "DeQueueFront",
expectedError: deque.ErrEmptyDequeue,
},
{
queryType: "DeQueueRear",
expectedError: deque.ErrEmptyDequeue,
},
{
queryType: "Length",
expectedResult: 0,
expectedError: nil,
},
},
},
{
description: "Test deque with one element",
queries: []QueryStructure[int]{
{
queryType: "EnQueueFront",
parameter: 1,
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: false,
expectedError: nil,
},
{
queryType: "Front",
expectedResult: 1,
expectedError: nil,
},
{
queryType: "Rear",
expectedResult: 1,
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 1,
expectedError: nil,
},
{
queryType: "DeQueueFront",
expectedResult: 1,
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: true,
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 0,
expectedError: nil,
},
},
},
{
description: "Test deque with multiple elements",
queries: []QueryStructure[int]{
{
queryType: "EnQueueFront",
parameter: 1,
expectedError: nil,
},
{
queryType: "EnQueueFront",
parameter: 2,
expectedError: nil,
},
{
queryType: "EnQueueRear",
parameter: 3,
expectedError: nil,
},
{
queryType: "EnQueueRear",
parameter: 4,
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: false,
expectedError: nil,
},
{
queryType: "Front",
expectedResult: 2,
expectedError: nil,
},
{
queryType: "Rear",
expectedResult: 4,
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 4,
expectedError: nil,
},
{
queryType: "DeQueueFront",
expectedResult: 2,
expectedError: nil,
},
{
queryType: "DeQueueRear",
expectedResult: 4,
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: false,
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 2,
expectedError: nil,
},
},
},
}
// Test cases with strings as params
testCasesString := []TestCaseData[string]{
{
description: "Test one element deque",
queries: []QueryStructure[string]{
{
queryType: "EnQueueFront",
parameter: "a",
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: false,
expectedError: nil,
},
{
queryType: "Front",
expectedResult: "a",
expectedError: nil,
},
{
queryType: "Rear",
expectedResult: "a",
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 1,
expectedError: nil,
},
{
queryType: "DeQueueFront",
expectedResult: "a",
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: true,
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 0,
expectedError: nil,
},
},
},
{
description: "Test multiple elements deque",
queries: []QueryStructure[string]{
{
queryType: "EnQueueFront",
parameter: "a",
expectedError: nil,
},
{
queryType: "EnQueueFront",
parameter: "b",
expectedError: nil,
},
{
queryType: "EnQueueRear",
parameter: "c",
expectedError: nil,
},
{
queryType: "EnQueueRear",
parameter: "d",
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: false,
expectedError: nil,
},
{
queryType: "Front",
expectedResult: "b",
expectedError: nil,
},
{
queryType: "Rear",
expectedResult: "d",
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 4,
expectedError: nil,
},
{
queryType: "DeQueueFront",
expectedResult: "b",
expectedError: nil,
},
{
queryType: "DeQueueRear",
expectedResult: "d",
expectedError: nil,
},
{
queryType: "IsEmpty",
expectedResult: false,
expectedError: nil,
},
{
queryType: "Length",
expectedResult: 2,
expectedError: nil,
},
},
},
}
// Run tests with ints
for _, testCase := range testCasesInt {
t.Run(testCase.description, func(t *testing.T) {
dq := deque.New[int]()
for _, query := range testCase.queries {
switch query.queryType {
case "EnQueueFront":
dq.EnqueueFront(query.parameter)
case "EnQueueRear":
dq.EnqueueRear(query.parameter)
case "DeQueueFront":
result, err := dq.DequeueFront()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "DeQueueRear":
result, err := dq.DequeueRear()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "Front":
result, err := dq.Front()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v, %v", query.expectedResult, result, testCase.description)
}
case "Rear":
result, err := dq.Rear()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "IsEmpty":
result := dq.IsEmpty()
if result != query.expectedResult {
t.Errorf("Expected error: %v, got : %v", query.expectedResult, result)
}
case "Length":
result := dq.Length()
if result != query.expectedResult {
t.Errorf("Expected %v got %v", query.expectedResult, result)
}
}
}
})
}
// Run tests with strings
for _, testCase := range testCasesString {
t.Run(testCase.description, func(t *testing.T) {
dq := deque.New[string]()
for _, query := range testCase.queries {
switch query.queryType {
case "EnQueueFront":
dq.EnqueueFront(query.parameter)
case "EnQueueRear":
dq.EnqueueRear(query.parameter)
case "DeQueueFront":
result, err := dq.DequeueFront()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "DeQueueRear":
result, err := dq.DequeueRear()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "Front":
result, err := dq.Front()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v, %v", query.expectedResult, result, testCase.description)
}
case "Rear":
result, err := dq.Rear()
if err != query.expectedError {
t.Errorf("Expected error: %v, got : %v", query.expectedError, err)
}
if err == nil && result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "IsEmpty":
result := dq.IsEmpty()
if result != query.expectedResult {
t.Errorf("Expected %v, got %v", query.expectedResult, result)
}
case "Length":
result := dq.Length()
if result != query.expectedResult {
t.Errorf("Expected %v got %v", query.expectedResult, result)
}
}
}
})
}
}
================================================
FILE: structure/doc.go
================================================
// Package structure is a subpackage that is dedicated to
// different implementations of data structures in the
// domain of computer science.
package structure
================================================
FILE: structure/dynamicarray/dynamicarray.go
================================================
// Package dynamicarray
// A dynamic array is quite similar to a regular array, but its Size is modifiable during program runtime,
// very similar to how a slice in Go works. The implementation is for educational purposes and explains
// how one might go about implementing their own version of slices.
//
// For more details check out those links below here:
// GeeksForGeeks article : https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
// Go blog: https://blog.golang.org/slices-intro
// Go blog: https://blog.golang.org/slices
// authors [Wesllhey Holanda](https://github.com/wesllhey), [Milad](https://github.com/miraddo)
// see dynamicarray.go, dynamicarray_test.go
package dynamicarray
import (
"errors"
)
var defaultCapacity = 10
// DynamicArray structure
type DynamicArray struct {
Size int
Capacity int
ElementData []any
}
// Put function is change/update the value in array with the index and new value
func (da *DynamicArray) Put(index int, element any) error {
err := da.CheckRangeFromIndex(index)
if err != nil {
return err
}
da.ElementData[index] = element
return nil
}
// Add function is add new element to our array
func (da *DynamicArray) Add(element any) {
if da.Size == da.Capacity {
da.NewCapacity()
}
da.ElementData[da.Size] = element
da.Size++
}
// Remove function is remove an element with the index
func (da *DynamicArray) Remove(index int) error {
err := da.CheckRangeFromIndex(index)
if err != nil {
return err
}
copy(da.ElementData[index:], da.ElementData[index+1:da.Size])
da.ElementData[da.Size-1] = nil
da.Size--
return nil
}
// Get function is return one element with the index of array
func (da *DynamicArray) Get(index int) (any, error) {
err := da.CheckRangeFromIndex(index)
if err != nil {
return nil, err
}
return da.ElementData[index], nil
}
// IsEmpty function is check that the array has value or not
func (da *DynamicArray) IsEmpty() bool {
return da.Size == 0
}
// GetData function return all value of array
func (da *DynamicArray) GetData() []any {
return da.ElementData[:da.Size]
}
// CheckRangeFromIndex function it will check the range from the index
func (da *DynamicArray) CheckRangeFromIndex(index int) error {
if index >= da.Size || index < 0 {
return errors.New("index out of range")
}
return nil
}
// NewCapacity function increase the Capacity
func (da *DynamicArray) NewCapacity() {
if da.Capacity == 0 {
da.Capacity = defaultCapacity
} else {
da.Capacity = da.Capacity << 1
}
newDataElement := make([]any, da.Capacity)
copy(newDataElement, da.ElementData)
da.ElementData = newDataElement
}
================================================
FILE: structure/dynamicarray/dynamicarray_test.go
================================================
package dynamicarray
import (
"reflect"
"testing"
)
func TestDynamicArray(t *testing.T) {
numbers := DynamicArray{}
// check numbers is empty or nut
t.Run("Check Empty Dynamic Array", func(t *testing.T) {
if numbers.IsEmpty() != true {
t.Errorf("Expected be true but got %v", numbers.IsEmpty())
}
})
numbers.Add(10)
numbers.Add(20)
numbers.Add(30)
numbers.Add(40)
numbers.Add(50)
// check numbers added to our dynamic array
t.Run("Add Element into Dynamic Array", func(t *testing.T) {
if numbers.IsEmpty() != false {
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
}
var res []any
res = append(res, 10)
res = append(res, 20)
res = append(res, 30)
res = append(res, 40)
res = append(res, 50)
if !reflect.DeepEqual(numbers.GetData(), res) {
t.Errorf("Expected be [10, 20, 30, 40, 50] but got %v", numbers.GetData())
}
})
// Remove an Element inside the dynamic array with the index of array
t.Run("Remove in Dynamic Array", func(t *testing.T) {
if numbers.IsEmpty() != false {
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
}
var res []any
res = append(res, 10)
res = append(res, 30)
res = append(res, 40)
res = append(res, 50)
// remove the element by the index
err := numbers.Remove(1)
if err != nil {
t.Errorf("Expected be [10, 30, 40, 50] but got an Error %v", err)
}
if !reflect.DeepEqual(numbers.GetData(), res) {
t.Errorf("Expected be [10, 30, 40, 50] but got %v", numbers.GetData())
}
})
// get one element by the index of the dynamic array
t.Run("Get in Dynamic Array", func(t *testing.T) {
if numbers.IsEmpty() != false {
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
}
// return one element with the index
getOne, _ := numbers.Get(2)
if getOne != 40 {
t.Errorf("Expected be 40 but got %v", getOne)
}
})
// Put to add a value to specific index of Dynamic Array
t.Run("Put to Dynamic Array", func(t *testing.T) {
if numbers.IsEmpty() != false {
t.Errorf("Expected be false but got %v", numbers.IsEmpty())
}
// change value of specific index
err := numbers.Put(0, 100)
if err != nil {
t.Errorf("Expected be [10, 30, 40, 50] but got an Error %v", err)
}
getOne, _ := numbers.Get(0)
if getOne != 100 {
t.Errorf("Expected be 100 but got %v", getOne)
}
})
}
================================================
FILE: structure/fenwicktree/fenwicktree.go
================================================
// Fenwick Tree Data Structure for efficient range queries on an array of integers.
// Also known as Binary Indexed Tree. It can query the sum of any range of the array and
// can update the array at a specific position by adding a value to it (point update).
// Build: O(N)
// Query: O(log(N))
// Update: O(log(N))
// reference: https://brilliant.org/wiki/fenwick-tree/
package fenwicktree
// FenwickTree represents the data structure of the Fenwick Tree
type FenwickTree struct {
n int // n: Size of the input array.
array []int // array: the input array on which queries are made.
bit []int // bit: store the sum of ranges.
}
// NewFenwickTree creates a new Fenwick tree, initializes bit with
// the values of the array. Note that the queries and updates should have
// one based indexing.
func NewFenwickTree(array []int) *FenwickTree {
newArray := []int{0} // Appending a 0 to the beginning as this implementation uses 1 based indexing
fenwickTree := &FenwickTree{
n: len(array),
array: append(newArray, array...),
bit: append(newArray, array...),
}
for i := 1; i < fenwickTree.n; i++ {
nextPos := i + (i & -i)
if nextPos <= fenwickTree.n {
fenwickTree.bit[nextPos] += fenwickTree.bit[i]
}
}
return fenwickTree
}
// PrefixSum returns the sum of the prefix ending at position pos.
func (f *FenwickTree) PrefixSum(pos int) int {
if pos > f.n {
return 0
}
prefixSum := 0
for i := pos; i > 0; i -= (i & -i) {
prefixSum += f.bit[i]
}
return prefixSum
}
// RangeSum returns the sum of the elements in the range l to r
// both inclusive.
func (f *FenwickTree) RangeSum(l int, r int) int {
return f.PrefixSum(r) - f.PrefixSum(l-1)
}
// Add Adds value to the element at position pos of the array
// and recomputes the range sums.
func (f *FenwickTree) Add(pos int, value int) {
for i := pos; i <= f.n; i += (i & -i) {
f.bit[i] += value
}
}
================================================
FILE: structure/fenwicktree/fenwicktree_test.go
================================================
package fenwicktree_test
import (
"github.com/TheAlgorithms/Go/structure/fenwicktree"
"testing"
)
type query struct {
queryType string
firstIndex int // firstIndex and lastIndex are same for point queries
lastIndex int
}
type update struct {
pos int
value int
}
func TestFenwickTree(t *testing.T) {
var fenwickTreeTestData = []struct {
description string
array []int
updates []update
queries []query
expected []int
}{
{
description: "test empty array",
array: []int{},
queries: []query{{"point", 1, 1}},
expected: []int{0},
},
{
description: "test array with size 5",
array: []int{1, 2, 3, 4, 5},
queries: []query{{"range", 1, 5}, {"range", 1, 3}, {"range", 3, 5}},
expected: []int{15, 6, 12},
},
{
description: "test array with size 5, single index updates and range queries",
array: []int{1, 2, 3, 4, 5},
updates: []update{{pos: 2, value: 2}, {pos: 3, value: 3}},
queries: []query{{"range", 1, 5}, {"range", 1, 3}, {"range", 3, 5}},
expected: []int{20, 11, 15},
},
{
description: "test array with size 5, single index updates and point queries",
array: []int{1, 2, 3, 4, 5},
updates: []update{{pos: 2, value: 2}, {pos: 3, value: 3}},
queries: []query{{"point", 3, 3}, {"point", 1, 1}, {"point", 5, 5}},
expected: []int{11, 1, 20},
},
}
for _, test := range fenwickTreeTestData {
t.Run(test.description, func(t *testing.T) {
fenwickTree := fenwicktree.NewFenwickTree(test.array)
for i := 0; i < len(test.updates); i++ {
fenwickTree.Add(test.updates[i].pos, test.updates[i].value)
}
for i := 0; i < len(test.queries); i++ {
var result int
if test.queries[i].queryType == "point" {
result = fenwickTree.PrefixSum(test.queries[i].firstIndex)
} else {
result = fenwickTree.RangeSum(test.queries[i].firstIndex, test.queries[i].lastIndex)
}
if result != test.expected[i] {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expected result: %d\nFound: %d\n", test.expected[i], result)
}
}
})
}
}
================================================
FILE: structure/hashmap/hashmap.go
================================================
package hashmap
import (
"fmt"
"hash/fnv"
)
var defaultCapacity uint64 = 1 << 10
type node struct {
key any
value any
next *node
}
// HashMap is a Golang implementation of a hashmap
type HashMap struct {
capacity uint64
size uint64
table []*node
}
// DefaultNew returns a new HashMap instance with default values
func DefaultNew() *HashMap {
return &HashMap{
capacity: defaultCapacity,
table: make([]*node, defaultCapacity),
}
}
// New creates a new HashMap instance with the specified size and capacity
func New(size, capacity uint64) *HashMap {
return &HashMap{
size: size,
capacity: capacity,
table: make([]*node, capacity),
}
}
// Get returns the value associated with the given key
func (hm *HashMap) Get(key any) any {
node := hm.getNodeByKey(key)
if node != nil {
return node.value
}
return nil
}
// Put inserts a new key-value pair into the hashmap
func (hm *HashMap) Put(key, value any) {
index := hm.hash(key)
if hm.table[index] == nil {
hm.table[index] = &node{key: key, value: value}
} else {
current := hm.table[index]
for {
if current.key == key {
current.value = value
return
}
if current.next == nil {
break
}
current = current.next
}
current.next = &node{key: key, value: value}
}
hm.size++
if float64(hm.size)/float64(hm.capacity) > 0.75 {
hm.resize()
}
}
// Contains checks if the given key is stored in the hashmap
func (hm *HashMap) Contains(key any) bool {
return hm.getNodeByKey(key) != nil
}
// getNodeByKey finds the node associated with the given key
func (hm *HashMap) getNodeByKey(key any) *node {
index := hm.hash(key)
current := hm.table[index]
for current != nil {
if current.key == key {
return current
}
current = current.next
}
return nil
}
// resize doubles the capacity of the hashmap and rehashes all existing entries
func (hm *HashMap) resize() {
oldTable := hm.table
hm.capacity <<= 1
hm.table = make([]*node, hm.capacity)
hm.size = 0
for _, head := range oldTable {
for current := head; current != nil; current = current.next {
hm.Put(current.key, current.value)
}
}
}
// hash generates a hash value for the given key
func (hm *HashMap) hash(key any) uint64 {
h := fnv.New64a()
_, _ = h.Write([]byte(fmt.Sprintf("%v", key)))
hashValue := h.Sum64()
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
}
================================================
FILE: structure/hashmap/hashmap_test.go
================================================
package hashmap_test
import (
"testing"
"github.com/TheAlgorithms/Go/structure/hashmap"
)
func TestHashMap(t *testing.T) {
mp := hashmap.DefaultNew()
t.Run("Test 1: Put(10) and checking if Get() is correct", func(t *testing.T) {
mp.Put("test", 10)
got := mp.Get("test")
if got != 10 {
t.Errorf("Put: %v, Got: %v", 10, got)
}
})
t.Run("Test 2: Reassiging the value and checking if its correct", func(t *testing.T) {
mp.Put("test", 20)
got := mp.Get("test")
if got != 20 {
t.Errorf("Put (reassign): %v, Got: %v", 20, got)
}
})
t.Run("Test 3: Adding new key when there is already some data", func(t *testing.T) {
mp.Put("test2", 30)
got := mp.Get("test2")
if got != 30 {
t.Errorf("Put: %v, Got: %v", got, 30)
}
})
t.Run("Test 4: Adding numeric key", func(t *testing.T) {
mp.Put(1, 40)
got := mp.Get(1)
if got != 40 {
t.Errorf("Put: %v, Got: %v", got, 40)
}
})
t.Run("Test 5: Checking the Contains function", func(t *testing.T) {
want := true
got := mp.Contains(1)
if want != got {
t.Errorf("Key '1' exists but couldn't be retrieved")
}
})
t.Run("Test 6: Checking if the key that does not exist returns false", func(t *testing.T) {
want := false
got := mp.Contains(2)
if got != want {
t.Errorf("Key '2' does not exist in the map but it says otherwise")
}
})
t.Run("Test 7: Checking if the key does not exist Get func returns nil", func(t *testing.T) {
want := any(nil)
got := mp.Get(2)
if got != want {
t.Errorf("Key '2' does not exists in the map but it says otherwise")
}
})
t.Run("Test 8: Resizing a map", func(t *testing.T) {
mp := hashmap.New(4, 4)
for i := 0; i < 20; i++ {
mp.Put(i, 40)
}
got := mp.Get(5)
if got != 40 {
t.Errorf("Put: %v, Got: %v", got, 40)
}
})
}
================================================
FILE: structure/heap/heap.go
================================================
package heap
import (
"errors"
"github.com/TheAlgorithms/Go/constraints"
)
// Heap heap implementation using generic.
type Heap[T any] struct {
heaps []T
lessFunc func(a, b T) bool
}
// New gives a new heap object.
func New[T constraints.Ordered]() *Heap[T] {
less := func(a, b T) bool {
return a < b
}
h, _ := NewAny[T](less)
return h
}
// NewAny gives a new heap object. element can be anything, but must provide less function.
func NewAny[T any](less func(a, b T) bool) (*Heap[T], error) {
if less == nil {
return nil, errors.New("less func is necessary")
}
return &Heap[T]{
lessFunc: less,
}, nil
}
// Push pushes the element t onto the heap.
// The complexity is O(log n) where n = h.Len().
func (h *Heap[T]) Push(t T) {
h.heaps = append(h.heaps, t)
h.up(len(h.heaps) - 1)
}
// Top returns the minimum element (according to Less) from the heap.
// Top panics if the heap is empty.
func (h *Heap[T]) Top() T {
return h.heaps[0]
}
// Pop removes the minimum element (according to Less) from the heap.
// The complexity is O(log n) where n = h.Len().
func (h *Heap[T]) Pop() {
if len(h.heaps) <= 1 {
h.heaps = nil
return
}
h.swap(0, len(h.heaps)-1)
h.heaps = h.heaps[:len(h.heaps)-1]
h.down(0)
}
// Empty returns the heap is empty or not.
func (h *Heap[T]) Empty() bool {
return len(h.heaps) == 0
}
// Size returns the size of the heap
func (h *Heap[T]) Size() int {
return len(h.heaps)
}
func (h *Heap[T]) swap(i, j int) {
h.heaps[i], h.heaps[j] = h.heaps[j], h.heaps[i]
}
func (h *Heap[T]) up(child int) {
if child <= 0 {
return
}
parent := (child - 1) >> 1
if !h.lessFunc(h.heaps[child], h.heaps[parent]) {
return
}
h.swap(child, parent)
h.up(parent)
}
func (h *Heap[T]) down(parent int) {
lessIdx := parent
lChild, rChild := (parent<<1)+1, (parent<<1)+2
if lChild < len(h.heaps) && h.lessFunc(h.heaps[lChild], h.heaps[lessIdx]) {
lessIdx = lChild
}
if rChild < len(h.heaps) && h.lessFunc(h.heaps[rChild], h.heaps[lessIdx]) {
lessIdx = rChild
}
if lessIdx == parent {
return
}
h.swap(lessIdx, parent)
h.down(lessIdx)
}
================================================
FILE: structure/heap/heap_test.go
================================================
package heap_test
import (
"github.com/TheAlgorithms/Go/structure/heap"
"reflect"
"testing"
)
type testInt int
func (u testInt) Less(o testInt) bool {
return u < o
}
type testStudent struct {
Name string
Score int64
}
func (u testStudent) Less(o testStudent) bool {
if u.Score == o.Score {
return u.Name < o.Name
}
return u.Score > o.Score
}
func TestHeap_Empty(t *testing.T) {
tests := []struct {
name string
want bool
}{
{name: "empty", want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := heap.New[testInt]()
if got := h.Empty(); got != tt.want {
t.Errorf("Empty() = %v, want %v", got, tt.want)
}
})
}
}
type testOpType int
const (
testPush = 1
testPop = 2
testTop = 3
testEmpty = 4
)
type testOp[T any] struct {
typ testOpType
x T
isEmpty bool
}
type testStruct[T any] struct {
name string
ops []testOp[T]
}
func TestHeapExample1(t *testing.T) {
tests1 := []testStruct[testInt]{
{
name: "example 1",
ops: []testOp[testInt]{
{typ: testEmpty, isEmpty: true},
{typ: testPush, x: 10},
{typ: testEmpty, isEmpty: false},
{typ: testTop, x: 10},
{typ: testPop},
{typ: testEmpty, isEmpty: true},
{typ: testPush, x: 9},
{typ: testPush, x: 8},
{typ: testPop},
{typ: testPush, x: 3},
{typ: testTop, x: 3},
{typ: testPush, x: 2},
{typ: testTop, x: 2},
{typ: testPush, x: 4},
{typ: testPush, x: 6},
{typ: testPush, x: 5},
{typ: testTop, x: 2},
{typ: testPop},
{typ: testTop, x: 3},
{typ: testPop},
{typ: testPop},
{typ: testTop, x: 5},
{typ: testEmpty, isEmpty: false},
},
},
}
testFunc(t, tests1, testInt.Less)
}
func TestHeapExample2(t *testing.T) {
tests1 := []testStruct[testStudent]{
{
name: "example 2",
ops: []testOp[testStudent]{
{typ: testPush, x: testStudent{Name: "Alan", Score: 87}},
{typ: testPush, x: testStudent{Name: "Bob", Score: 98}},
{typ: testTop, x: testStudent{Name: "Bob", Score: 98}},
{typ: testPop},
{typ: testPush, x: testStudent{Name: "Carl", Score: 70}},
{typ: testTop, x: testStudent{Name: "Alan", Score: 87}},
},
},
}
testFunc(t, tests1, testStudent.Less)
}
func testFunc[T any](t *testing.T, tests []testStruct[T], less func(a, b T) bool) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h, err := heap.NewAny[T](less)
if err != nil {
t.Errorf("New Heap err %v", err)
}
for i, op := range tt.ops {
switch op.typ {
case testPush:
oldSize := h.Size()
h.Push(op.x)
newSize := h.Size()
if oldSize+1 != newSize {
t.Errorf("op %d testPush %v failed", i, op.x)
}
case testPop:
oldSize := h.Size()
h.Pop()
newSize := h.Size()
if oldSize-1 != newSize {
t.Errorf("op %d testPop %v failed", i, op.x)
}
case testTop:
if got := h.Top(); !reflect.DeepEqual(got, op.x) {
t.Errorf("op %d testTop %v, want %v", i, got, op.x)
}
case testEmpty:
if got := h.Empty(); got != op.isEmpty {
t.Errorf("op %d Empty() = %v, want %v", i, got, op.isEmpty)
}
}
}
})
}
}
================================================
FILE: structure/linkedlist/Readme.md
================================================
# Linked List
***
## What is it?
***
[Linked list](https://www.scaler.com/topics/linked-list/) is a data structure that is a linear chain of data elements whose order is not given by their phyisical placement in memory. This structure is built up of nodes which point to the element ahead or behind this particular node (depending on the type of Linked List).
# Types of Linked List implemented within this repository
## Singly Linked List
Singly Linked List is a linked list in which a node only points to the next element.
Some of the main applications of Singly Linked List are in the construction
of fundamental data structures such as Stacks and Queues.
## Doubly Linked List
Doubly Linked List is a linked list in which a node points to both the element ahead and behind the node.
Any application which requires us to keep track of forward and backward information uses doubly linked list.
For example, the feature of undo and redo are implemented using these doubly linked lists.
## Cyclic Linked List AKA Looped Linked List
Looped Linked Lists are singly or doubly-linked that chase their own tail:
A points to B, B points to C, C points to D, and D points to A.
They are better suited for cyclic data such as train schedules.
These lists are missing the first and last items.
Therefore, it is necessary to introduce the concept of the current position.
This picture shows similar lists:

================================================
FILE: structure/linkedlist/cyclic.go
================================================
package linkedlist
import "fmt"
// Cyclic Struct which cycles the linked list in this implementation.
type Cyclic[T any] struct {
Size int
Head *Node[T]
}
// Create new list.
func NewCyclic[T any]() *Cyclic[T] {
return &Cyclic[T]{}
}
// Inserting the first node is a special case. It will
// point to itself. For other cases, the node will be added
// to the end of the list. End of the list is Prev field of
// current item. Complexity O(1).
func (cl *Cyclic[T]) Add(val T) {
n := NewNode(val)
cl.Size++
if cl.Head == nil {
n.Prev = n
n.Next = n
cl.Head = n
} else {
n.Prev = cl.Head.Prev
n.Next = cl.Head
cl.Head.Prev.Next = n
cl.Head.Prev = n
}
}
// Rotate list by P places.
// This method is interesting for optimization.
// For first optimization we must decrease
// P value so that it ranges from 0 to N-1.
// For this we need to use the operation of
// division modulo. But be careful if P is less than 0.
// if it is - make it positive. This can be done without
// violating the meaning of the number by adding to it
// a multiple of N. Now you can decrease P modulo N to
// rotate the list by the minimum number of places.
// We use the fact that moving forward in a circle by P
// places is the same as moving N - P places back.
// Therefore, if P > N / 2, you can turn the list by N-P places back.
// Complexity O(n).
func (cl *Cyclic[T]) Rotate(places int) {
if cl.Size > 0 {
if places < 0 {
multiple := cl.Size - 1 - places/cl.Size
places += multiple * cl.Size
}
places %= cl.Size
if places > cl.Size/2 {
places = cl.Size - places
for i := 0; i < places; i++ {
cl.Head = cl.Head.Prev
}
} else if places == 0 {
return
} else {
for i := 0; i < places; i++ {
cl.Head = cl.Head.Next
}
}
}
}
// Delete the current item.
func (cl *Cyclic[T]) Delete() bool {
var deleted bool
var prevItem, thisItem, nextItem *Node[T]
if cl.Size == 0 {
return deleted
}
deleted = true
thisItem = cl.Head
nextItem = thisItem.Next
prevItem = thisItem.Prev
if cl.Size == 1 {
cl.Head = nil
} else {
cl.Head = nextItem
nextItem.Prev = prevItem
prevItem.Next = nextItem
}
cl.Size--
return deleted
}
// Destroy all items in the list.
func (cl *Cyclic[T]) Destroy() {
for cl.Delete() {
continue
}
}
// Show list body.
func (cl *Cyclic[T]) Walk() *Node[T] {
var start *Node[T]
start = cl.Head
for i := 0; i < cl.Size; i++ {
fmt.Printf("%v \n", start.Val)
start = start.Next
}
return start
}
// https://en.wikipedia.org/wiki/Josephus_problem
// This is a struct-based solution for Josephus problem.
func JosephusProblem(cl *Cyclic[int], k int) int {
for cl.Size > 1 {
cl.Rotate(k)
cl.Delete()
cl.Rotate(-1)
}
retval := cl.Head.Val
cl.Destroy()
return retval
}
================================================
FILE: structure/linkedlist/cyclic_test.go
================================================
package linkedlist
import (
"reflect"
"testing"
)
func fillList(list *Cyclic[int], n int) {
for i := 1; i <= n; i++ {
list.Add(i)
}
}
func TestAdd(t *testing.T) {
list := NewCyclic[int]()
fillList(list, 3)
want := []any{1, 2, 3}
var got []any
var start *Node[int]
start = list.Head
for i := 0; i < list.Size; i++ {
got = append(got, start.Val)
start = start.Next
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
}
func TestWalk(t *testing.T) {
list := NewCyclic[int]()
fillList(list, 3)
want := 1
got := list.Walk()
if got.Val != want {
t.Errorf("got: %v, want: nil", got)
}
}
func TestRotate(t *testing.T) {
type testCase struct {
param int
wantToReturn int
}
list := NewCyclic[int]()
fillList(list, 3)
testCases := []testCase{
{1, 2},
{3, 2},
{6, 2},
{7, 3},
{-2, 1},
{5, 3},
{8, 2},
{-8, 3},
}
for idx, tCase := range testCases {
list.Rotate(tCase.param)
got := list.Head.Val
if got != tCase.wantToReturn {
t.Errorf("got: %v, want: %v for test id %v", got, tCase.wantToReturn, idx)
}
}
}
func TestDelete(t *testing.T) {
list := NewCyclic[int]()
fillList(list, 3)
want := 2
wantSize := 2
list.Delete()
got := list.Head.Val
if want != got {
t.Errorf("got: %v, want: %v", got, want)
}
if wantSize != list.Size {
t.Errorf("got: %v, want: %v", got, want)
}
}
func TestDestroy(t *testing.T) {
list := NewCyclic[int]()
fillList(list, 3)
wantSize := 0
list.Destroy()
got := list.Head
if got != nil {
t.Errorf("got: %v, want: nil", got)
}
if wantSize != list.Size {
t.Errorf("got: %v, want: %v", got, wantSize)
}
}
func TestJosephusProblem(t *testing.T) {
type testCase struct {
param int
wantToReturn int
listCount int
}
testCases := []testCase{
{5, 4, 8},
{3, 8, 8},
{8, 5, 8},
{8, 5, 8},
{2, 14, 14},
{13, 56, 58},
{7, 5, 5},
}
for _, tCase := range testCases {
list := NewCyclic[int]()
fillList(list, tCase.listCount)
got := JosephusProblem(list, tCase.param)
if got != tCase.wantToReturn {
t.Errorf("got: %v, want: %v", got, tCase.wantToReturn)
}
}
}
================================================
FILE: structure/linkedlist/doc.go
================================================
// Package linkedlist demonstrates different implementations on linkedlists.
package linkedlist
================================================
FILE: structure/linkedlist/doubly.go
================================================
package linkedlist
import "fmt"
// Doubly structure with just the Head Node
// We call it `Doubly` to make it easier to
// understand when calling this in peoples
// own local code to understand and experiment
// with this easily.
// For example, we can use gotools `go get` command to get
// this repository cloned inside the
// $GOPATH/src/github.com/TheAlgorithms/Go (you can do this
// manually as well) and use the import statement as follows:
//
// `import "github.com/TheAlgorithms/Go/structure/linkedlist"`
//
// and call linkedlist.Doubly to create a new doubly linked list.
type Doubly[T any] struct {
Head *Node[T]
}
// Init initializes double linked list
func (ll *Doubly[T]) Init() *Doubly[T] {
ll.Head = &Node[T]{}
ll.Head.Next = ll.Head
ll.Head.Prev = ll.Head
return ll
}
func NewDoubly[T any]() *Doubly[T] {
return new(Doubly[T]).Init()
}
// lazyInit lazily initializes a zero List value.
func (ll *Doubly[T]) lazyInit() {
if ll.Head.Next == nil {
ll.Init()
}
}
func (ll *Doubly[T]) insert(n, at *Node[T]) *Node[T] {
n.Prev = at
n.Next = at.Next
n.Prev.Next = n
n.Next.Prev = n
return n
}
func (ll *Doubly[T]) insertValue(val T, at *Node[T]) *Node[T] {
return ll.insert(NewNode(val), at)
}
// AddAtBeg Add a node to the beginning of the linkedlist
func (ll *Doubly[T]) AddAtBeg(val T) {
ll.lazyInit()
ll.insertValue(val, ll.Head)
}
// AddAtEnd Add a node at the end of the linkedlist
func (ll *Doubly[T]) AddAtEnd(val T) {
ll.lazyInit()
ll.insertValue(val, ll.Head.Prev)
}
func (ll *Doubly[T]) Remove(n *Node[T]) T {
n.Prev.Next = n.Next
n.Next.Prev = n.Prev
n.Next = nil
n.Prev = nil
return n.Val
}
// DelAtBeg Delete the node at the beginning of the linkedlist
func (ll *Doubly[T]) DelAtBeg() (T, bool) {
// no item
if ll.Head.Next == nil {
var r T
return r, false
}
n := ll.Head.Next
val := n.Val
ll.Remove(n)
return val, true
}
// DetAtEnd Delete a node at the end of the linkedlist
func (ll *Doubly[T]) DelAtEnd() (T, bool) {
// no item
if ll.Head.Prev == nil {
var r T
return r, false
}
n := ll.Head.Prev
val := n.Val
ll.Remove(n)
return val, true
}
// DelByPos deletes node at middle based on position in list
// and returns value. If empty or position of node is less than linked list length, returns false
func (ll *Doubly[T]) DelByPos(pos int) (T, bool) {
switch {
case ll.Head == nil:
var r T
return r, false
case pos-1 == 0:
return ll.DelAtBeg()
case pos-1 == ll.Count():
return ll.DelAtEnd()
case pos-1 > ll.Count():
var r T
return r, false
}
var prev *Node[T]
var val T
cur := ll.Head
count := 0
for count < pos-1 {
prev = cur
cur = cur.Next
count++
}
cur.Next.Prev = prev
val = cur.Val
prev.Next = cur.Next
return val, true
}
// Count Number of nodes in the linkedlist
func (ll *Doubly[T]) Count() int {
var ctr int = 0
if ll.Head.Next == nil {
return 0
}
for cur := ll.Head.Next; cur != ll.Head; cur = cur.Next {
ctr += 1
}
return ctr
}
// Reverse Reverse the order of the linkedlist
func (ll *Doubly[T]) Reverse() {
var Prev, Next *Node[T]
cur := ll.Head
for cur != nil {
Next = cur.Next
cur.Next = Prev
cur.Prev = Next
Prev = cur
cur = Next
}
ll.Head = Prev
}
// Display display the linked list
func (ll *Doubly[T]) Display() {
for cur := ll.Head.Next; cur != ll.Head; cur = cur.Next {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}
// DisplayReverse Display the linkedlist in reverse order
func (ll *Doubly[T]) DisplayReverse() {
if ll.Head == nil {
return
}
var cur *Node[T]
for cur = ll.Head.Prev; cur != ll.Head; cur = cur.Prev {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}
func (ll *Doubly[T]) Front() *Node[T] {
if ll.Count() == 0 {
return nil
}
return ll.Head.Next
}
func (ll *Doubly[T]) Back() *Node[T] {
if ll.Count() == 0 {
return nil
}
return ll.Head.Prev
}
func (ll *Doubly[T]) MoveToBack(n *Node[T]) {
if ll.Head.Prev == n {
return
}
ll.move(n, ll.Head.Prev)
}
func (ll *Doubly[T]) move(n, at *Node[T]) {
if n == at {
return
}
n.Prev.Next = n.Next
n.Next.Prev = n.Prev
n.Prev = at
n.Next = at.Next
n.Prev.Next = n
n.Next.Prev = n
}
================================================
FILE: structure/linkedlist/doubly_test.go
================================================
package linkedlist
import (
"reflect"
"testing"
)
func TestDoubly(t *testing.T) {
newList := NewDoubly[int]()
newList.AddAtBeg(1)
newList.AddAtBeg(2)
newList.AddAtBeg(3)
t.Run("Test AddAtBeg", func(t *testing.T) {
wantNext := []int{3, 2, 1}
wantPrev := []int{1, 2, 3}
got := []int{}
// check from Next address
current := newList.Head.Next
got = append(got, current.Val)
for current.Next != newList.Head {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, wantNext) {
t.Errorf("got: %v, want: %v", got, wantNext)
}
// check from Prev address
got = []int{}
got = append(got, current.Val)
for current.Prev != newList.Head {
current = current.Prev
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, wantPrev) {
t.Errorf("got: %v, want: %v", got, wantPrev)
}
})
newList.AddAtEnd(4)
t.Run("Test AddAtEnd", func(t *testing.T) {
want := []int{3, 2, 1, 4}
got := []int{}
current := newList.Head.Next
got = append(got, current.Val)
for current.Next != newList.Head {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test DelAtBeg", func(t *testing.T) {
want := 3
got, ok := newList.DelAtBeg()
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test DelAtEnd", func(t *testing.T) {
want := 4
got, ok := newList.DelAtEnd()
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test Count", func(t *testing.T) {
want := 2
got := newList.Count()
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
newList2 := NewDoubly[int]()
newList2.AddAtBeg(1)
newList2.AddAtBeg(2)
newList2.AddAtBeg(3)
t.Run("Test Reverse", func(t *testing.T) {
want := []int{1, 2, 3}
got := []int{}
newList2.Reverse()
current := newList2.Head.Next
got = append(got, current.Val)
for current.Next != newList2.Head {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
}
================================================
FILE: structure/linkedlist/shared.go
================================================
package linkedlist
// Node Structure representing the linkedlist node.
// This node is shared across different implementations.
type Node[T any] struct {
Val T
Prev *Node[T]
Next *Node[T]
}
// Create new node.
func NewNode[T any](val T) *Node[T] {
return &Node[T]{val, nil, nil}
}
================================================
FILE: structure/linkedlist/singlylinkedlist.go
================================================
package linkedlist
// demonstration of singly linked list in golang
import (
"errors"
"fmt"
)
// Singly structure with length of the list and its head
type Singly[T any] struct {
length int
// Note that Node here holds both Next and Prev Node
// however only the Next node is used in Singly methods.
Head *Node[T]
}
// NewSingly returns a new instance of a linked list
func NewSingly[T any]() *Singly[T] {
return &Singly[T]{}
}
// AddAtBeg adds a new snode with given value at the beginning of the list.
func (ll *Singly[T]) AddAtBeg(val T) {
n := NewNode(val)
n.Next = ll.Head
ll.Head = n
ll.length++
}
// AddAtEnd adds a new snode with given value at the end of the list.
func (ll *Singly[T]) AddAtEnd(val T) {
n := NewNode(val)
if ll.Head == nil {
ll.Head = n
ll.length++
return
}
cur := ll.Head
for ; cur.Next != nil; cur = cur.Next {
}
cur.Next = n
ll.length++
}
// DelAtBeg deletes the snode at the head(beginning) of the list
// and returns its value. Returns false if the list is empty.
func (ll *Singly[T]) DelAtBeg() (T, bool) {
if ll.Head == nil {
var r T
return r, false
}
cur := ll.Head
ll.Head = cur.Next
ll.length--
return cur.Val, true
}
// DelAtEnd deletes the snode at the tail(end) of the list
// and returns its value. Returns false if the list is empty.
func (ll *Singly[T]) DelAtEnd() (T, bool) {
if ll.Head == nil {
var r T
return r, false
}
if ll.Head.Next == nil {
return ll.DelAtBeg()
}
cur := ll.Head
for ; cur.Next.Next != nil; cur = cur.Next {
}
retval := cur.Next.Val
cur.Next = nil
ll.length--
return retval, true
}
// DelByPos deletes the node at the middle based on position in the list
// and returns its value. Returns false if the list is empty or length is not more than given position
func (ll *Singly[T]) DelByPos(pos int) (T, bool) {
switch {
case ll.Head == nil:
var r T
return r, false
case pos-1 > ll.length:
var r T
return r, false
case pos-1 == 0:
return ll.DelAtBeg()
case pos-1 == ll.Count():
return ll.DelAtEnd()
}
var prev *Node[T]
var val T
cur := ll.Head
count := 0
for count < pos-1 {
prev = cur
cur = cur.Next
count++
}
val = cur.Val
prev.Next = cur.Next
ll.length--
return val, true
}
// Count returns the current size of the list.
func (ll *Singly[T]) Count() int {
return ll.length
}
// Reverse reverses the list.
func (ll *Singly[T]) Reverse() {
var prev, next *Node[T]
cur := ll.Head
for cur != nil {
next = cur.Next
cur.Next = prev
prev = cur
cur = next
}
ll.Head = prev
}
// ReversePartition Reverse the linked list from the ath to the bth node
func (ll *Singly[T]) ReversePartition(left, right int) error {
err := ll.CheckRangeFromIndex(left, right)
if err != nil {
return err
}
tmpNode := &Node[T]{}
tmpNode.Next = ll.Head
pre := tmpNode
for i := 0; i < left-1; i++ {
pre = pre.Next
}
cur := pre.Next
for i := 0; i < right-left; i++ {
next := cur.Next
cur.Next = next.Next
next.Next = pre.Next
pre.Next = next
}
ll.Head = tmpNode.Next
return nil
}
func (ll *Singly[T]) CheckRangeFromIndex(left, right int) error {
if left > right {
return errors.New("left boundary must smaller than right")
} else if left < 1 {
return errors.New("left boundary starts from the first node")
} else if right > ll.length {
return errors.New("right boundary cannot be greater than the length of the linked list")
}
return nil
}
// Display prints out the elements of the list.
func (ll *Singly[T]) Display() {
for cur := ll.Head; cur != nil; cur = cur.Next {
fmt.Print(cur.Val, " ")
}
fmt.Print("\n")
}
================================================
FILE: structure/linkedlist/singlylinkedlist_test.go
================================================
package linkedlist
import (
"reflect"
"testing"
)
func TestSingly(t *testing.T) {
list := NewSingly[int]()
list.AddAtBeg(1)
list.AddAtBeg(2)
list.AddAtBeg(3)
t.Run("Test AddAtBeg()", func(t *testing.T) {
want := []any{3, 2, 1}
got := []any{}
current := list.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
list.AddAtEnd(4)
t.Run("Test AddAtEnd()", func(t *testing.T) {
want := []any{3, 2, 1, 4}
got := []any{}
current := list.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test DelAtBeg()", func(t *testing.T) {
want := any(3)
got, ok := list.DelAtBeg()
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test DelAtEnd()", func(t *testing.T) {
want := any(4)
got, ok := list.DelAtEnd()
if !ok {
t.Error("unexpected not-ok")
}
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test Count()", func(t *testing.T) {
want := 2
got := list.Count()
if got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
list2 := Singly[int]{}
list2.AddAtBeg(1)
list2.AddAtBeg(2)
list2.AddAtBeg(3)
list2.AddAtBeg(4)
list2.AddAtBeg(5)
list2.AddAtBeg(6)
t.Run("Test Reverse()", func(t *testing.T) {
want := []any{1, 2, 3, 4, 5, 6}
got := []any{}
list2.Reverse()
current := list2.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
t.Run("Test ReversePartition()", func(t *testing.T) {
want := []any{1, 5, 4, 3, 2, 6}
got := []any{}
err := list2.ReversePartition(2, 5)
if err != nil {
t.Errorf("Incorrect boundary conditions entered%v", err)
}
current := list2.Head
got = append(got, current.Val)
for current.Next != nil {
current = current.Next
got = append(got, current.Val)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
}
================================================
FILE: structure/queue/queue_test.go
================================================
// Queue Test
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
// The order is First In First Out (FIFO).
// details:
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see queuearray.go, queuelinkedlist.go, queuelinkedlistwithlist.go
package queue
import (
"container/list"
"testing"
)
func TestQueue(t *testing.T) {
// Handle Queue Linked List
t.Run("Test Queue Linked List", func(t *testing.T) {
t.Run("Test EnQueue", func(t *testing.T) {
var newQueue Queue
newQueue.enqueue(2)
newQueue.enqueue(3)
newQueue.enqueue(4)
newQueue.enqueue(45)
if newQueue.frontQueue() != 2 && newQueue.backQueue() != 45 {
t.Errorf("Test EnQueue is wrong the result must be %v and %v but got %v and %v", 2, 45, newQueue.frontQueue(), newQueue.backQueue())
}
})
t.Run("Test DeQueue", func(t *testing.T) {
var newQueue Queue
newQueue.enqueue(2)
newQueue.enqueue(3)
newQueue.enqueue(4)
newQueue.dequeue()
if newQueue.dequeue() != 3 {
t.Errorf("Test DeQueue is wrong the result must be %v but got %v", 3, newQueue.dequeue())
}
//fmt.Println(newQueue.show())
})
t.Run("Test Queue isEmpty", func(t *testing.T) {
var newQueue Queue
if newQueue.isEmpty() != true {
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", true, newQueue.isEmpty())
}
newQueue.enqueue(3)
newQueue.enqueue(4)
if newQueue.isEmpty() != false {
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", false, newQueue.isEmpty())
}
})
t.Run("Test Queue Length", func(t *testing.T) {
var newQueue Queue
if newQueue.len() != 0 {
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 0, newQueue.len())
}
newQueue.enqueue(3)
newQueue.enqueue(4)
newQueue.dequeue()
newQueue.enqueue(22)
newQueue.enqueue(99)
newQueue.dequeue()
newQueue.dequeue()
if newQueue.len() != 1 {
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 1, newQueue.len())
}
})
})
// Handle Queue Array
t.Run("Test Queue Array", func(t *testing.T) {
t.Run("Test EnQueue", func(t *testing.T) {
EnQueue(2)
EnQueue(23)
EnQueue(45)
EnQueue(66)
if FrontQueue() != 2 && BackQueue() != 66 {
t.Errorf("Test EnQueue is wrong the result must be %v and %v but got %v and %v", 2, 66, FrontQueue(), BackQueue())
}
})
t.Run("Test DeQueue", func(t *testing.T) {
EnQueue(2)
EnQueue(23)
EnQueue(45)
EnQueue(66)
DeQueue()
DeQueue()
if DeQueue() != 45 {
t.Errorf("Test DeQueue is wrong the result must be %v but got %v", 45, DeQueue())
}
})
ListQueue = []any{}
t.Run("Test Queue isEmpty", func(t *testing.T) {
if IsEmptyQueue() != true {
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", true, IsEmptyQueue())
}
EnQueue(3)
EnQueue(4)
if IsEmptyQueue() != false {
t.Errorf("Test Queue isEmpty is wrong the result must be %v but got %v", false, IsEmptyQueue())
}
})
ListQueue = []any{}
t.Run("Test Queue Length", func(t *testing.T) {
if LenQueue() != 0 {
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 0, LenQueue())
}
EnQueue(3)
EnQueue(4)
DeQueue()
EnQueue(22)
EnQueue(99)
DeQueue()
DeQueue()
if LenQueue() != 1 {
t.Errorf("Test Queue Length is wrong the result must be %v but got %v", 1, LenQueue())
}
})
})
// Handle Queue Linked-List With Container/List (STL)
t.Run("Test Container/List For Queue", func(t *testing.T) {
listQueue := &LQueue{
queue: list.New(),
}
t.Run("List Enqueue", func(t *testing.T) {
listQueue.Enqueue("Snap")
listQueue.Enqueue(123)
listQueue.Enqueue(true)
listQueue.Enqueue(212.545454)
if listQueue.Len() != 4 {
t.Errorf("List Enqueue is not correct expected %d but got %d", 4, listQueue.Len())
}
})
t.Run("List Dequeue", func(t *testing.T) {
err := listQueue.Dequeue()
if err != nil {
t.Error("got an unexpected error ", err)
}
if listQueue.Len() != 3 {
t.Errorf("List Dequeue is not correct expected %d but got %d", 3, listQueue.Len())
}
})
t.Run("List Front", func(t *testing.T) {
err := listQueue.Dequeue()
if err != nil {
t.Error("got an unexpected error ", err)
}
result, err := listQueue.Front()
if err != nil {
t.Error("got an unexpected error ", err)
}
if result != true {
t.Errorf("List Front is not correct expected %v but got %v", true, result)
}
})
t.Run("List Back", func(t *testing.T) {
err := listQueue.Dequeue()
if err != nil {
t.Error("got an unexpected error ", err)
}
result, err := listQueue.Back()
if err != nil {
t.Error("got an unexpected error ", err)
}
if result != 212.545454 {
t.Errorf("List Back is not correct expected %v but got %v", 212.545454, result)
}
})
t.Run("List Length", func(t *testing.T) {
listQueue.Enqueue("Snap")
err := listQueue.Dequeue()
if err != nil {
t.Error("got an unexpected error ", err)
}
if listQueue.Len() != 1 {
t.Errorf("List Length is not correct expected %v but got %v", 1, listQueue.Len())
}
})
t.Run("List Empty", func(t *testing.T) {
err := listQueue.Dequeue()
if err != nil {
t.Error("got an unexpected error ", err)
}
if !listQueue.Empty() {
t.Errorf("List Empty is not correct expected %v but got %v", true, listQueue.Empty())
}
})
})
}
================================================
FILE: structure/queue/queuearray.go
================================================
// Queue Array
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
// The order is First In First Out (FIFO).
// details:
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see queuelinkedlist.go, queuelinkedlistwithlist.go, queue_test.go
package queue
var ListQueue []any
// EnQueue it will be added new value into our list
func EnQueue(n any) {
ListQueue = append(ListQueue, n)
}
// DeQueue it will be removed the first value that added into the list
func DeQueue() any {
data := ListQueue[0]
ListQueue = ListQueue[1:]
return data
}
// FrontQueue return the Front value
func FrontQueue() any {
return ListQueue[0]
}
// BackQueue return the Back value
func BackQueue() any {
return ListQueue[len(ListQueue)-1]
}
// LenQueue will return the length of the queue list
func LenQueue() int {
return len(ListQueue)
}
// IsEmptyQueue check our list is empty or not
func IsEmptyQueue() bool {
return len(ListQueue) == 0
}
================================================
FILE: structure/queue/queuelinkedlist.go
================================================
// Queue Linked-List
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
// The order is First In First Out (FIFO).
// details:
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see queuearray.go, queuelinkedlistwithlist.go, queue_test.go
package queue
// Node will be store the value and the next node as well
type Node struct {
Data any
Next *Node
}
// Queue structure is tell us what our head is and what tail should be with length of the list
type Queue struct {
head *Node
tail *Node
length int
}
// enqueue it will be added new value into queue
func (ll *Queue) enqueue(n any) {
var newNode Node // create new Node
newNode.Data = n // set the data
if ll.tail != nil {
ll.tail.Next = &newNode
}
ll.tail = &newNode
if ll.head == nil {
ll.head = &newNode
}
ll.length++
}
// dequeue it will be removed the first value into queue (First In First Out)
func (ll *Queue) dequeue() any {
if ll.isEmpty() {
return -1 // if is empty return -1
}
data := ll.head.Data
ll.head = ll.head.Next
if ll.head == nil {
ll.tail = nil
}
ll.length--
return data
}
// isEmpty it will check our list is empty or not
func (ll *Queue) isEmpty() bool {
return ll.length == 0
}
// len is return the length of queue
func (ll *Queue) len() int {
return ll.length
}
// frontQueue it will return the front data
func (ll *Queue) frontQueue() any {
return ll.head.Data
}
// backQueue it will return the back data
func (ll *Queue) backQueue() any {
return ll.tail.Data
}
================================================
FILE: structure/queue/queuelinklistwithlist.go
================================================
// Queue Linked-List with standard library (Container/List)
// description: based on `geeksforgeeks` description A Queue is a linear structure which follows a particular order in which the operations are performed.
// The order is First In First Out (FIFO).
// details:
// Queue Data Structure : https://www.geeksforgeeks.org/queue-data-structure/
// Queue (abstract data type) : https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see queuearray.go, queuelinkedlist.go, queue_test.go
package queue
// container/list: is used as linked-list
// fmt: used to return fmt.Errorf for the error part
import (
"container/list"
"fmt"
)
// LQueue will be store the value into the list
type LQueue struct {
queue *list.List
}
// Enqueue will be added new value
func (lq *LQueue) Enqueue(value any) {
lq.queue.PushBack(value)
}
// Dequeue will be removed the first value that input (First In First Out - FIFO)
func (lq *LQueue) Dequeue() error {
if !lq.Empty() {
element := lq.queue.Front()
lq.queue.Remove(element)
return nil
}
return fmt.Errorf("dequeue is empty we got an error")
}
// Front it will return the front value
func (lq *LQueue) Front() (any, error) {
if !lq.Empty() {
val := lq.queue.Front().Value
return val, nil
}
return "", fmt.Errorf("error queue is empty")
}
// Back it will return the back value
func (lq *LQueue) Back() (any, error) {
if !lq.Empty() {
val := lq.queue.Back().Value
return val, nil
}
return "", fmt.Errorf("error queue is empty")
}
// Len it will return the length of list
func (lq *LQueue) Len() int {
return lq.queue.Len()
}
// Empty is check our list is empty or not
func (lq *LQueue) Empty() bool {
return lq.queue.Len() == 0
}
================================================
FILE: structure/segmenttree/segmenttree.go
================================================
// Segment Tree Data Structure for efficient range queries on an array of integers.
// It can query the sum and update the elements to a new value of any range of the array.
// Build: O(n*log(n))
// Query: O(log(n))
// Update: O(log(n))
// reference: https://cp-algorithms.com/data_structures/segment_tree.html
package segmenttree
import (
"github.com/TheAlgorithms/Go/math/max"
"github.com/TheAlgorithms/Go/math/min"
)
const emptyLazyNode = 0
// SegmentTree represents the data structure of a segment tree with lazy propagation
type SegmentTree struct {
Array []int // The original array
SegmentTree []int // Stores the sum of different ranges
LazyTree []int // Stores the values of lazy propagation
}
// Propagate propagates the lazy updates to the child nodes
func (s *SegmentTree) Propagate(node int, leftNode int, rightNode int) {
if s.LazyTree[node] != emptyLazyNode {
//add lazy node value multiplied by (right-left+1), which represents all interval
//this is the same of adding a value on each node
s.SegmentTree[node] += (rightNode - leftNode + 1) * s.LazyTree[node]
if leftNode == rightNode {
//leaf node
s.Array[leftNode] += s.LazyTree[node]
} else {
//propagate lazy node value for children nodes
//may propagate multiple times, children nodes should accumulate lazy node value
s.LazyTree[2*node] += s.LazyTree[node]
s.LazyTree[2*node+1] += s.LazyTree[node]
}
//clear lazy node
s.LazyTree[node] = emptyLazyNode
}
}
// Query returns the sum of elements of the array in the interval [firstIndex, leftIndex].
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Query(node int, leftNode int, rightNode int, firstIndex int, lastIndex int) int {
if (firstIndex > lastIndex) || (leftNode > rightNode) {
//outside the interval
return 0
}
//propagate lazy tree
s.Propagate(node, leftNode, rightNode)
if (leftNode >= firstIndex) && (rightNode <= lastIndex) {
//inside the interval
return s.SegmentTree[node]
}
//get sum of left and right nodes
mid := (leftNode + rightNode) / 2
leftNodeSum := s.Query(2*node, leftNode, mid, firstIndex, min.Int(mid, lastIndex))
rightNodeSum := s.Query(2*node+1, mid+1, rightNode, max.Int(firstIndex, mid+1), lastIndex)
return leftNodeSum + rightNodeSum
}
// Update updates the elements of the array in the range [firstIndex, lastIndex]
// with the new value provided and recomputes the sum of different ranges.
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Update(node int, leftNode int, rightNode int, firstIndex int, lastIndex int, value int) {
//propagate lazy tree
s.Propagate(node, leftNode, rightNode)
if (firstIndex > lastIndex) || (leftNode > rightNode) {
//outside the interval
return
}
if (leftNode >= firstIndex) && (rightNode <= lastIndex) {
//inside the interval
//accumulate the lazy node value
s.LazyTree[node] += value
s.Propagate(node, leftNode, rightNode)
} else {
//update left and right nodes
mid := (leftNode + rightNode) / 2
s.Update(2*node, leftNode, mid, firstIndex, min.Int(mid, lastIndex), value)
s.Update(2*node+1, mid+1, rightNode, max.Int(firstIndex, mid+1), lastIndex, value)
s.SegmentTree[node] = s.SegmentTree[2*node] + s.SegmentTree[2*node+1]
}
}
// Build builds the SegmentTree by computing the sum of different ranges.
// node, leftNode and rightNode should always start with 1, 0 and len(Array)-1, respectively.
func (s *SegmentTree) Build(node int, left int, right int) {
if left == right {
//leaf node
s.SegmentTree[node] = s.Array[left]
} else {
//get sum of left and right nodes
mid := (left + right) / 2
s.Build(2*node, left, mid)
s.Build(2*node+1, mid+1, right)
s.SegmentTree[node] = s.SegmentTree[2*node] + s.SegmentTree[2*node+1]
}
}
// NewSegmentTree returns a new instance of a SegmentTree. It takes an input
// array of integers representing Array, initializes and builds the SegmentTree.
func NewSegmentTree(Array []int) *SegmentTree {
if len(Array) == 0 {
return nil
}
segTree := SegmentTree{
Array: Array,
SegmentTree: make([]int, 4*len(Array)),
LazyTree: make([]int, 4*len(Array)),
}
for i := range segTree.LazyTree {
//fill LazyTree with empty lazy nodes
segTree.LazyTree[i] = emptyLazyNode
}
//starts with node 1 and interval [0, len(arr)-1] inclusive
segTree.Build(1, 0, len(Array)-1)
return &segTree
}
================================================
FILE: structure/segmenttree/segmenttree_test.go
================================================
package segmenttree
import (
"testing"
)
// Query interval
type query struct {
firstIndex int
lastIndex int
}
type update struct {
firstIndex int
lastIndex int
value int
}
func TestSegmentTree(t *testing.T) {
var segmentTreeTestData = []struct {
description string
array []int
updates []update
queries []query
expected []int
}{
{
description: "test empty array",
array: []int{},
queries: []query{{0, 0}},
expected: []int{0},
},
{
description: "test array with size 5",
array: []int{1, 2, 3, 4, 5},
queries: []query{{0, 5}, {0, 2}, {2, 4}},
expected: []int{15, 6, 12},
},
{
description: "test array with size 5 and updates",
array: []int{1, 2, 3, 4, 5},
updates: []update{{firstIndex: 1, lastIndex: 1, value: 2},
{firstIndex: 2, lastIndex: 2, value: 3}},
queries: []query{{0, 5}, {0, 2}, {2, 4}},
expected: []int{20, 11, 15},
},
{
description: "test array with size 5 and single index updates",
array: []int{1, 2, 3, 4, 5},
updates: []update{{firstIndex: 1, lastIndex: 1, value: 2},
{firstIndex: 2, lastIndex: 2, value: 3}},
queries: []query{{0, 5}, {0, 2}, {2, 4}},
expected: []int{20, 11, 15},
},
{
description: "test array with size 5 and range updates",
array: []int{1, 2, 3, 4, 5},
updates: []update{{firstIndex: 0, lastIndex: 4, value: 2},
{firstIndex: 2, lastIndex: 4, value: 2}},
queries: []query{{0, 5}, {0, 2}, {2, 4}},
expected: []int{31, 14, 24},
},
{
description: "test array with size 11 and range updates",
array: []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
updates: []update{{firstIndex: 2, lastIndex: 8, value: 2},
{firstIndex: 2, lastIndex: 8, value: 2}},
queries: []query{{3, 5}, {7, 8}, {4, 5}, {8, 8}},
expected: []int{15, 10, 10, 5},
},
}
for _, test := range segmentTreeTestData {
t.Run(test.description, func(t *testing.T) {
segmentTree := NewSegmentTree(test.array)
for i := 0; i < len(test.updates); i++ {
segmentTree.Update(1, 0, len(test.array)-1, test.updates[i].firstIndex, test.updates[i].lastIndex, test.updates[i].value)
}
for i := 0; i < len(test.queries); i++ {
result := segmentTree.Query(1, 0, len(test.array)-1, test.queries[i].firstIndex, test.queries[i].lastIndex)
if result != test.expected[i] {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expected result: %d\nFound: %d\n", test.expected[i], result)
}
}
})
}
}
================================================
FILE: structure/set/set.go
================================================
// package set implements a Set using a golang map.
// This implies that only the types that are accepted as valid map keys can be used as set elements.
// For instance, do not try to Add a slice, or the program will panic.
package set
// New gives new set.
func New[T comparable](items ...T) Set[T] {
st := set[T]{
elements: make(map[T]bool),
}
for _, item := range items {
st.Add(item)
}
return &st
}
// Set is an interface of possible methods on 'set'.
type Set[T comparable] interface {
// Add: adds new element to the set
Add(item T)
// Delete: deletes the passed element from the set if present
Delete(item T)
// Len: gives the length of the set (total no. of elements in set)
Len() int
// GetItems: gives the array( []T ) of elements of the set.
GetItems() []T
// In: checks whether item is present in set or not.
In(item T) bool
// IsSubsetOf: checks whether set is subset of set2 or not.
IsSubsetOf(set2 Set[T]) bool
// IsProperSubsetOf: checks whether set is proper subset of set2 or not.
// ex: [1,2,3] proper subset of [1,2,3,4] -> true
IsProperSubsetOf(set2 Set[T]) bool
// IsSupersetOf: checks whether set is superset of set2 or not.
IsSupersetOf(set2 Set[T]) bool
// IsProperSupersetOf: checks whether set is proper superset of set2 or not.
// ex: [1,2,3,4] proper superset of [1,2,3] -> true
IsProperSupersetOf(set2 Set[T]) bool
// Union: gives new union set of both sets.
// ex: [1,2,3] union [3,4,5] -> [1,2,3,4,5]
Union(set2 Set[T]) Set[T]
// Intersection: gives new intersection set of both sets.
// ex: [1,2,3] Intersection [3,4,5] -> [3]
Intersection(set2 Set[T]) Set[T]
// Difference: gives new difference set of both sets.
// ex: [1,2,3] Difference [3,4,5] -> [1,2]
Difference(set2 Set[T]) Set[T]
// SymmetricDifference: gives new symmetric difference set of both sets.
// ex: [1,2,3] SymmetricDifference [3,4,5] -> [1,2,4,5]
SymmetricDifference(set2 Set[T]) Set[T]
}
type set[T comparable] struct {
elements map[T]bool
}
func (st *set[T]) Add(value T) {
st.elements[value] = true
}
func (st *set[T]) Delete(value T) {
delete(st.elements, value)
}
func (st *set[T]) GetItems() []T {
keys := make([]T, 0, len(st.elements))
for k := range st.elements {
keys = append(keys, k)
}
return keys
}
func (st *set[T]) Len() int {
return len(st.elements)
}
func (st *set[T]) In(value T) bool {
if _, in := st.elements[value]; in {
return true
}
return false
}
func (st *set[T]) IsSubsetOf(superSet Set[T]) bool {
if st.Len() > superSet.Len() {
return false
}
for _, item := range st.GetItems() {
if !superSet.In(item) {
return false
}
}
return true
}
func (st *set[T]) IsProperSubsetOf(superSet Set[T]) bool {
if st.Len() == superSet.Len() {
return false
}
return st.IsSubsetOf(superSet)
}
func (st *set[T]) IsSupersetOf(subSet Set[T]) bool {
return subSet.IsSubsetOf(st)
}
func (st *set[T]) IsProperSupersetOf(subSet Set[T]) bool {
if st.Len() == subSet.Len() {
return false
}
return st.IsSupersetOf(subSet)
}
func (st *set[T]) Union(st2 Set[T]) Set[T] {
unionSet := New[T]()
for _, item := range st.GetItems() {
unionSet.Add(item)
}
for _, item := range st2.GetItems() {
unionSet.Add(item)
}
return unionSet
}
func (st *set[T]) Intersection(st2 Set[T]) Set[T] {
intersectionSet := New[T]()
var minSet, maxSet Set[T]
if st.Len() > st2.Len() {
minSet = st2
maxSet = st
} else {
minSet = st
maxSet = st2
}
for _, item := range minSet.GetItems() {
if maxSet.In(item) {
intersectionSet.Add(item)
}
}
return intersectionSet
}
func (st *set[T]) Difference(st2 Set[T]) Set[T] {
differenceSet := New[T]()
for _, item := range st.GetItems() {
if !st2.In(item) {
differenceSet.Add(item)
}
}
return differenceSet
}
func (st *set[T]) SymmetricDifference(st2 Set[T]) Set[T] {
symmetricDifferenceSet := New[T]()
dropSet := New[T]()
for _, item := range st.GetItems() {
if st2.In(item) {
dropSet.Add(item)
} else {
symmetricDifferenceSet.Add(item)
}
}
for _, item := range st2.GetItems() {
if !dropSet.In(item) {
symmetricDifferenceSet.Add(item)
}
}
return symmetricDifferenceSet
}
================================================
FILE: structure/set/set_test.go
================================================
package set
import (
"testing"
)
func TestNew(t *testing.T) {
set := New(1, 2, 3)
if set.Len() != 3 {
t.Errorf("expecting 3 elements in the set but got %v: %v", set.Len(), set.GetItems())
}
for _, n := range []int{1, 2, 3} {
if !set.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, set.GetItems())
}
}
if set.In(5) {
t.Errorf("expecting 5 not to be present in the set but it was; set is %v", set.GetItems())
}
}
func TestAdd(t *testing.T) {
td := []struct {
name string
input int
expElems []int
}{
{"add new element", 5, []int{1, 2, 3, 5}},
{"add exiting element", 3, []int{1, 2, 3}},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := New(1, 2, 3)
s.Add(tc.input)
if s.Len() != len(tc.expElems) {
t.Errorf("expecting %d elements in the set but got %d: set is %v", len(tc.expElems), s.Len(), s.GetItems())
}
for _, n := range tc.expElems {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestDelete(t *testing.T) {
td := []struct {
name string
input int
expElems []int
}{
{"delete exiting element", 3, []int{1, 2}},
{"delete not existing element", 5, []int{1, 2, 3}},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := New(1, 2, 3)
s.Delete(tc.input)
if s.Len() != len(tc.expElems) {
t.Errorf("expecting %d elements in the set but got %d: set is %v", len(tc.expElems), s.Len(), s.GetItems())
}
for _, n := range tc.expElems {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestIsSubsetOf(t *testing.T) {
s1, s2 := New(1, 2, 3), New(1, 2, 3, 4)
if !s1.IsSubsetOf(s2) {
t.Errorf("expecting %v to be a subset of %v", s1, s2)
}
if s2.IsSubsetOf(s1) {
t.Errorf("expecting %v not to be a subset of %v", s2, s1)
}
if s3 := New(1, 2, 5, 6); s1.IsSubsetOf(s3) {
t.Errorf("expecting %v not to be a subset of %v", s1, s3)
}
}
func TestIsProperSubsetOf(t *testing.T) {
s1, s2 := New(1, 2, 3), New(1, 2, 3, 4)
if !s1.IsProperSubsetOf(s2) {
t.Errorf("expecting %v to be a proper subset of %v", s1, s2)
}
if s3 := New(3, 2, 1); s1.IsProperSubsetOf(s3) {
t.Errorf("expecting %v not to be a proper subset of %v", s1, s3)
}
}
func TestIsSupersetOf(t *testing.T) {
s1, s2 := New(1, 2, 3), New(1, 2, 3, 4)
if !s2.IsSupersetOf(s1) {
t.Errorf("expecting %v to be a superset of %v", s2, s1)
}
if s1.IsSupersetOf(s2) {
t.Errorf("expecting %v not to be a superset of %v", s1, s2)
}
if s3 := New(1, 2, 5); s2.IsSupersetOf(s3) {
t.Errorf("expecting %v not to be a superset of %v", s2, s3)
}
}
func TestIsProperSupersetOf(t *testing.T) {
s1, s2 := New(1, 2, 3), New(1, 2, 3, 4)
if !s2.IsProperSupersetOf(s1) {
t.Errorf("expecting %v to be a proper superset of %v", s2, s1)
}
if s3 := New(3, 2, 1); s1.IsProperSupersetOf(s3) {
t.Errorf("expecting %v not to be a proper superset of %v", s1, s3)
}
}
func TestUnion(t *testing.T) {
td := []struct {
name string
s1 Set[int]
s2 Set[int]
expSet Set[int]
}{
{"union of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3, 4, 5, 6)},
{"union of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(1, 2, 3, 4)},
{"union of same sets", New(1, 2, 3), New(1, 2, 3), New(1, 2, 3)},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.Union(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestIntersection(t *testing.T) {
td := []struct {
name string
s1 Set[int]
s2 Set[int]
expSet Set[int]
}{
{"intersection of different sets", New(0, 1, 2, 3), New(4, 5, 6), New[int]()},
{"intersection of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(1, 2)},
{"intersection of same sets", New(1, 2, 3), New(1, 2, 3), New(1, 2, 3)},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.Intersection(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestDifference(t *testing.T) {
td := []struct {
name string
s1 Set[int]
s2 Set[int]
expSet Set[int]
}{
{"difference of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3)},
{"difference of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(3)},
{"difference of same sets", New(1, 2, 3), New(1, 2, 3), New[int]()},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.Difference(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
func TestSymmetricDifference(t *testing.T) {
td := []struct {
name string
s1 Set[int]
s2 Set[int]
expSet Set[int]
}{
{"symmetric difference of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3, 4, 5, 6)},
{"symmetric difference of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(3, 4)},
{"symmetric difference of same sets", New(1, 2, 3), New(1, 2, 3), New[int]()},
}
for _, tc := range td {
t.Run(tc.name, func(t *testing.T) {
s := tc.s1.SymmetricDifference(tc.s2)
if s.Len() != tc.expSet.Len() {
t.Errorf("expecting %d elements in the set but got %d: set is %v", tc.expSet.Len(), s.Len(), s.GetItems())
}
for _, n := range tc.expSet.GetItems() {
if !s.In(n) {
t.Errorf("expecting %d to be present in the set but was not; set is %v", n, s.GetItems())
}
}
})
}
}
================================================
FILE: structure/set/setexample_test.go
================================================
package set
import (
"fmt"
)
func ExampleSet() {
set := New(1, 2, 3)
fmt.Println(set.Len()) // 3
set.Add(3)
fmt.Println(set.Len()) // 3
set.Add(4)
fmt.Println(set.Len()) // 4
// output:
// 3
// 3
// 4
}
================================================
FILE: structure/stack/stack_test.go
================================================
// Stack Test
// description: based on `geeksforgeeks` description Stack is a linear data structure which follows a particular order in which the operations are performed.
// The order may be LIFO(Last In First Out) or FILO(First In Last Out).
// details:
// Stack Data Structure : https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
// Stack (abstract data type) : https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see stackarray.go, stacklinkedlist.go, stacklinkedlistwithlist.go
package stack_test
import (
"container/list"
"github.com/TheAlgorithms/Go/structure/stack"
"reflect"
"testing"
)
// TestStackLinkedList for testing Stack with LinkedList
func TestStackLinkedList(t *testing.T) {
var newStack stack.Stack
newStack.Push(1)
newStack.Push(2)
t.Run("Stack Push", func(t *testing.T) {
result := newStack.Show()
expected := []any{2, 1}
for x := range result {
if result[x] != expected[x] {
t.Errorf("Stack Push is not work, got %v but expected %v", result, expected)
}
}
})
t.Run("Stack isEmpty", func(t *testing.T) {
if newStack.IsEmpty() {
t.Error("Stack isEmpty is returned true but expected false", newStack.IsEmpty())
}
})
t.Run("Stack Length", func(t *testing.T) {
if newStack.Length() != 2 {
t.Error("Stack Length should be 2 but got", newStack.Length())
}
})
newStack.Pop()
pop := newStack.Pop()
t.Run("Stack Pop", func(t *testing.T) {
if pop != 1 {
t.Error("Stack Pop should return 1 but is returned", pop)
}
})
newStack.Push(52)
newStack.Push(23)
newStack.Push(99)
t.Run("Stack Peek", func(t *testing.T) {
if newStack.Peek() != 99 {
t.Error("Stack Peak should return 99 but got ", newStack.Peek())
}
})
}
// TestStackArray for testing Stack with Array
func TestStackArray(t *testing.T) {
newStack := stack.NewStack[int]()
t.Run("Stack With Array", func(t *testing.T) {
newStack.Push(2)
newStack.Push(3)
t.Run("Stack Push", func(t *testing.T) {
var stackElements []any
for i := 0; i < 2; i++ {
poppedElement := newStack.Pop()
stackElements = append(stackElements, poppedElement)
}
if !reflect.DeepEqual([]any{3, 2}, stackElements) {
t.Errorf("Stack Push is not work we expected %v but got %v", []any{3, 2}, newStack)
}
newStack.Push(2)
newStack.Push(3)
})
pop := newStack.Pop()
t.Run("Stack Pop", func(t *testing.T) {
if newStack.Length() == 2 && pop != 3 {
t.Errorf("Stack Pop is not work we expected %v but got %v", 3, pop)
}
})
newStack.Push(2)
newStack.Push(83)
t.Run("Stack Peak", func(t *testing.T) {
if newStack.Peek() != 83 {
t.Errorf("Stack Peek is not work we expected %v but got %v", 83, newStack.Peek())
}
})
t.Run("Stack Length", func(t *testing.T) {
if newStack.Length() != 3 {
t.Errorf("Stack Length is not work we expected %v but got %v", 3, newStack.Length())
}
})
t.Run("Stack Empty", func(t *testing.T) {
if newStack.IsEmpty() == true {
t.Errorf("Stack Empty is not work we expected %v but got %v", false, newStack.IsEmpty())
}
newStack.Pop()
newStack.Pop()
newStack.Pop()
if newStack.IsEmpty() == false {
t.Errorf("Stack Empty is not work we expected %v but got %v", true, newStack.IsEmpty())
}
})
})
}
// TestStackLinkedListWithList for testing Stack with Container/List Library (STL)
func TestStackLinkedListWithList(t *testing.T) {
stackList := &stack.SList{
Stack: list.New(),
}
t.Run("Stack Push", func(t *testing.T) {
stackList.Push(2)
stackList.Push(3)
if stackList.Length() != 2 {
t.Errorf("Stack Push is not work we expected %v but got %v", 2, stackList.Length())
}
})
t.Run("Stack Pop", func(t *testing.T) {
pop, _ := stackList.Pop()
if stackList.Length() == 1 && pop != 3 {
t.Errorf("Stack Pop is not work we expected %v but got %v", 3, pop)
}
})
t.Run("Stack Peak", func(t *testing.T) {
stackList.Push(2)
stackList.Push(83)
peak, _ := stackList.Peek()
if peak != 83 {
t.Errorf("Stack Peak is not work we expected %v but got %v", 83, peak)
}
})
t.Run("Stack Length", func(t *testing.T) {
if stackList.Length() != 3 {
t.Errorf("Stack Length is not work we expected %v but got %v", 3, stackList.Length())
}
})
t.Run("Stack Empty", func(t *testing.T) {
if stackList.IsEmpty() == true {
t.Errorf("Stack Empty is not work we expected %v but got %v", false, stackList.IsEmpty())
}
d1, err := stackList.Pop()
d2, _ := stackList.Pop()
d3, _ := stackList.Pop()
if err != nil {
t.Errorf("got an unexpected error %v, pop1: %v, pop2: %v, pop3: %v", err, d1, d2, d3)
}
if stackList.IsEmpty() == false {
t.Errorf("Stack Empty is not work we expected %v but got %v", true, stackList.IsEmpty())
}
})
}
================================================
FILE: structure/stack/stackarray.go
================================================
// Stack Array
// description: based on `geeksforgeeks` description Stack is a linear data structure which follows a particular order in which the operations are performed.
// The order may be LIFO(Last In First Out) or FILO(First In Last Out).
// details:
// Stack Data Structure : https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
// Stack (abstract data type) : https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see stacklinkedlist.go, stacklinkedlistwithlist.go, stack_test.go
package stack
type Array[T any] struct {
elements []T
}
// NewStack creates and returns a new stack.
func NewStack[T any]() *Array[T] {
return &Array[T]{}
}
// Push adds an element to the top of the stack.
func (s *Array[T]) Push(value T) {
s.elements = append(s.elements, value)
}
// Size returns the number of elements in the stack.
func (s *Array[T]) Length() int {
return len(s.elements)
}
// Peek returns the top element of the stack without removing it.
func (s *Array[T]) Peek() T {
if s.IsEmpty() {
var zeroValue T
return zeroValue // Stack is empty
}
return s.elements[len(s.elements)-1]
}
// IsEmpty returns true if the stack is empty, false otherwise.
func (s *Array[T]) IsEmpty() bool {
return len(s.elements) == 0
}
// Pop removes and returns the top element from the stack.
func (s *Array[T]) Pop() T {
if s.IsEmpty() {
var zeroValue T
return zeroValue // Stack is empty
}
index := len(s.elements) - 1
popped := s.elements[index]
s.elements = s.elements[:index]
return popped
}
================================================
FILE: structure/stack/stacklinkedlist.go
================================================
// Stack Linked-List
// description: based on `geeksforgeeks` description Stack is a linear data structure which follows a particular order in which the operations are performed.
// The order may be LIFO(Last In First Out) or FILO(First In Last Out).
// details:
// Stack Data Structure : https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
// Stack (abstract data type) : https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see stacklinkedlistwithlist.go, stackarray.go, stack_test.go
package stack
// Node structure
type Node struct {
Val any
Next *Node
}
// Stack has jost top of node and with length
type Stack struct {
top *Node
length int
}
// push add value to last index
func (ll *Stack) Push(n any) {
newStack := &Node{} // new node
newStack.Val = n
newStack.Next = ll.top
ll.top = newStack
ll.length++
}
// pop remove last item as first output
func (ll *Stack) Pop() any {
result := ll.top.Val
if ll.top.Next == nil {
ll.top = nil
} else {
ll.top.Val, ll.top.Next = ll.top.Next.Val, ll.top.Next.Next
}
ll.length--
return result
}
// isEmpty to check our array is empty or not
func (ll *Stack) IsEmpty() bool {
return ll.length == 0
}
// len use to return length of our stack
func (ll *Stack) Length() int {
return ll.length
}
// peak return last input value
func (ll *Stack) Peek() any {
return ll.top.Val
}
// show all value as an interface array
func (ll *Stack) Show() (in []any) {
current := ll.top
for current != nil {
in = append(in, current.Val)
current = current.Next
}
return
}
================================================
FILE: structure/stack/stacklinkedlistwithlist.go
================================================
// Stack Linked-List with standard library (Container/List)
// description: based on `geeksforgeeks` description Stack is a linear data structure which follows a particular order in which the operations are performed.
// The order may be LIFO(Last In First Out) or FILO(First In Last Out).
// details:
// Stack Data Structure : https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
// Stack (abstract data type) : https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
// author [Milad](https://github.com/miraddo)
// see stacklinkedlist.go, stackarray.go, stack_test.go
package stack
import (
"container/list"
"fmt"
)
// SList is our struct that point to stack with container/list.List library
type SList struct {
Stack *list.List
}
// Push add a value into our stack
func (sl *SList) Push(val any) {
sl.Stack.PushFront(val)
}
// Peak is return last value that insert into our stack
func (sl *SList) Peek() (any, error) {
if !sl.IsEmpty() {
element := sl.Stack.Front()
return element.Value, nil
}
return "", fmt.Errorf("stack list is empty")
}
// Pop is return last value that insert into our stack
// also it will remove it in our stack
func (sl *SList) Pop() (any, error) {
if !sl.IsEmpty() {
// get last element that insert into stack
element := sl.Stack.Front()
// remove element in stack
sl.Stack.Remove(element)
// return element value
return element.Value, nil
}
return "", fmt.Errorf("stack list is empty")
}
// Length return length of our stack
func (sl *SList) Length() int {
return sl.Stack.Len()
}
// Empty check our stack has value or not
func (sl *SList) IsEmpty() bool {
// check our stack is empty or not
// if is 0 it means our stack is empty otherwise is not empty
return sl.Stack.Len() == 0
}
================================================
FILE: structure/structure_test.go
================================================
// Empty test file to keep track of all the tests for the algorithms.
package structure
================================================
FILE: structure/tree/avl.go
================================================
// AVL tree is a self-balancing binary search tree.
//
// For more details check out those link below here:
// Wikipedia article: https://en.wikipedia.org/wiki/AVL_tree
// see avl.go
package tree
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/max"
)
// Verify Interface Compliance
var _ Node[int] = &AVLNode[int]{}
// AVLNode represents a single node in the AVL.
type AVLNode[T constraints.Ordered] struct {
key T
parent *AVLNode[T]
left *AVLNode[T]
right *AVLNode[T]
height int
}
func (n *AVLNode[T]) Key() T {
return n.key
}
func (n *AVLNode[T]) Parent() Node[T] {
return n.parent
}
func (n *AVLNode[T]) Left() Node[T] {
return n.left
}
func (n *AVLNode[T]) Right() Node[T] {
return n.right
}
func (n *AVLNode[T]) Height() int {
return n.height
}
// AVL represents a AVL tree.
// By default, _NIL = nil.
type AVL[T constraints.Ordered] struct {
Root *AVLNode[T]
_NIL *AVLNode[T] // a sentinel value for nil
}
// NewAVL creates a novel AVL tree
func NewAVL[T constraints.Ordered]() *AVL[T] {
return &AVL[T]{
Root: nil,
_NIL: nil,
}
}
// Empty determines the AVL tree is empty
func (avl *AVL[T]) Empty() bool {
return avl.Root == avl._NIL
}
// Push a chain of Node's into the AVL Tree
func (avl *AVL[T]) Push(keys ...T) {
for _, k := range keys {
avl.Root = avl.pushHelper(avl.Root, k)
}
}
// Delete a Node from the AVL Tree
func (avl *AVL[T]) Delete(key T) bool {
if !avl.Has(key) {
return false
}
avl.Root = avl.deleteHelper(avl.Root, key)
return true
}
// Get a Node from the AVL Tree
func (avl *AVL[T]) Get(key T) (Node[T], bool) {
return searchTreeHelper[T](avl.Root, avl._NIL, key)
}
// Has Determines the tree has the node of Key
func (avl *AVL[T]) Has(key T) bool {
_, ok := searchTreeHelper[T](avl.Root, avl._NIL, key)
return ok
}
// PreOrder Traverses the tree in the following order Root --> Left --> Right
func (avl *AVL[T]) PreOrder() []T {
traversal := make([]T, 0)
preOrderRecursive[T](avl.Root, avl._NIL, &traversal)
return traversal
}
// InOrder Traverses the tree in the following order Left --> Root --> Right
func (avl *AVL[T]) InOrder() []T {
return inOrderHelper[T](avl.Root, avl._NIL)
}
// PostOrder traverses the tree in the following order Left --> Right --> Root
func (avl *AVL[T]) PostOrder() []T {
traversal := make([]T, 0)
postOrderRecursive[T](avl.Root, avl._NIL, &traversal)
return traversal
}
// LevelOrder returns the level order traversal of the tree
func (avl *AVL[T]) LevelOrder() []T {
traversal := make([]T, 0)
levelOrderHelper[T](avl.Root, avl._NIL, &traversal)
return traversal
}
// AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.
func (avl *AVL[T]) AccessNodesByLayer() [][]T {
return accessNodeByLayerHelper[T](avl.Root, avl._NIL)
}
// Depth returns the calculated depth of the AVL tree
func (avl *AVL[T]) Depth() int {
return calculateDepth[T](avl.Root, avl._NIL, 0)
}
// Max returns the Max value of the tree
func (avl *AVL[T]) Max() (T, bool) {
ret := maximum[T](avl.Root, avl._NIL)
if ret == avl._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Min returns the Min value of the tree
func (avl *AVL[T]) Min() (T, bool) {
ret := minimum[T](avl.Root, avl._NIL)
if ret == avl._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Predecessor returns the Predecessor of the node of Key
// if there is no predecessor, return default value of type T and false
// otherwise return the Key of predecessor and true
func (avl *AVL[T]) Predecessor(key T) (T, bool) {
node, ok := searchTreeHelper[T](avl.Root, avl._NIL, key)
if !ok {
var dft T
return dft, ok
}
return predecessorHelper[T](node, avl._NIL)
}
// Successor returns the Successor of the node of Key
// if there is no successor, return default value of type T and false
// otherwise return the Key of successor and true
func (avl *AVL[T]) Successor(key T) (T, bool) {
node, ok := searchTreeHelper[T](avl.Root, avl._NIL, key)
if !ok {
var dft T
return dft, ok
}
return successorHelper[T](node, avl._NIL)
}
func (avl *AVL[T]) pushHelper(root *AVLNode[T], key T) *AVLNode[T] {
if root == avl._NIL {
return &AVLNode[T]{
key: key,
left: avl._NIL,
right: avl._NIL,
parent: avl._NIL,
height: 1,
}
}
switch {
case key < root.key:
tmp := avl.pushHelper(root.left, key)
tmp.parent = root
root.left = tmp
case key > root.key:
tmp := avl.pushHelper(root.right, key)
tmp.parent = root
root.right = tmp
default:
return root
}
// balance the tree
root.height = avl.height(root)
bFactor := avl.balanceFactor(root)
if bFactor > 1 {
switch {
case key < root.left.key:
return avl.rightRotate(root)
case key > root.left.key:
root.left = avl.leftRotate(root.left)
return avl.rightRotate(root)
}
}
if bFactor < -1 {
switch {
case key > root.right.key:
return avl.leftRotate(root)
case key < root.right.key:
root.right = avl.rightRotate(root.right)
return avl.leftRotate(root)
}
}
return root
}
func (avl *AVL[T]) deleteHelper(root *AVLNode[T], key T) *AVLNode[T] {
if root == avl._NIL {
return root
}
switch {
case key < root.key:
tmp := avl.deleteHelper(root.left, key)
root.left = tmp
if tmp != avl._NIL {
tmp.parent = root
}
case key > root.key:
tmp := avl.deleteHelper(root.right, key)
root.right = tmp
if tmp != avl._NIL {
tmp.parent = root
}
default:
if root.left == avl._NIL || root.right == avl._NIL {
tmp := root.left
if root.right != avl._NIL {
tmp = root.right
}
if tmp == avl._NIL {
root = avl._NIL
} else {
tmp.parent = root.parent
root = tmp
}
} else {
tmp := minimum[T](root.right, avl._NIL).(*AVLNode[T])
root.key = tmp.key
del := avl.deleteHelper(root.right, tmp.key)
root.right = del
if del != avl._NIL {
del.parent = root
}
}
}
if root == avl._NIL {
return root
}
// balance the tree
root.height = avl.height(root)
bFactor := avl.balanceFactor(root)
switch {
case bFactor > 1:
switch {
case avl.balanceFactor(root.left) >= 0:
return avl.rightRotate(root)
default:
root.left = avl.leftRotate(root.left)
return avl.rightRotate(root)
}
case bFactor < -1:
switch {
case avl.balanceFactor(root.right) <= 0:
return avl.leftRotate(root)
default:
root.right = avl.rightRotate(root.right)
return avl.leftRotate(root)
}
}
return root
}
func (avl *AVL[T]) height(root *AVLNode[T]) int {
if root == avl._NIL {
return 1
}
var leftHeight, rightHeight int
if root.left != avl._NIL {
leftHeight = root.left.height
}
if root.right != avl._NIL {
rightHeight = root.right.height
}
return 1 + max.Int(leftHeight, rightHeight)
}
// balanceFactor : negative balance factor means subtree Root is heavy toward Left
// and positive balance factor means subtree Root is heavy toward Right side
func (avl *AVL[T]) balanceFactor(root *AVLNode[T]) int {
var leftHeight, rightHeight int
if root.left != avl._NIL {
leftHeight = root.left.height
}
if root.right != avl._NIL {
rightHeight = root.right.height
}
return leftHeight - rightHeight
}
func (avl *AVL[T]) leftRotate(x *AVLNode[T]) *AVLNode[T] {
y := x.right
yl := y.left
y.left = x
x.right = yl
if yl != avl._NIL {
yl.parent = x
}
y.parent = x.parent
x.parent = y
x.height = avl.height(x)
y.height = avl.height(y)
return y
}
func (avl *AVL[T]) rightRotate(x *AVLNode[T]) *AVLNode[T] {
y := x.left
yr := y.right
y.right = x
x.left = yr
if yr != avl._NIL {
yr.parent = x
}
y.parent = x.parent
x.parent = y
x.height = avl.height(x)
y.height = avl.height(y)
return y
}
================================================
FILE: structure/tree/avl_test.go
================================================
package tree_test
import (
bt "github.com/TheAlgorithms/Go/structure/tree"
"math/rand"
"sort"
"testing"
"time"
)
func TestAVLPush(t *testing.T) {
t.Run("LLRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(5, 4, 3)
root := tree.Root
if root.Key() != 4 {
t.Errorf("Root should have value = 4, not %v", root.Key())
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2, not %d", root.Height())
}
if root.Left().Key() != 3 {
t.Errorf("Left child should have value = 3")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 5 {
t.Errorf("Right child should have value = 5")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("LRRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(5, 3, 4)
root := tree.Root
if root.Key() != 4 {
t.Errorf("Root should have value = 4, not %v", root.Key())
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2, not %d", root.Height())
}
if root.Left().Key() != 3 {
t.Errorf("Left child should have value = 3")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 5 {
t.Errorf("Right child should have value = 5")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("RRRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(3)
tree.Push(4)
tree.Push(5)
root := tree.Root
if root.Key() != 4 {
t.Errorf("Root should have value = 4, not %v", root.Key())
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2, not %d", root.Height())
}
if root.Left().Key() != 3 {
t.Errorf("Left child should have value = 3")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 5 {
t.Errorf("Right child should have value = 5")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("RLRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(3)
tree.Push(5)
tree.Push(4)
root := tree.Root
if root.Key() != 4 {
t.Errorf("Root should have value = 4")
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2")
}
if root.Left().Key() != 3 {
t.Errorf("Left child should have value = 3")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 5 {
t.Errorf("Right child should have value = 5")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
}
func TestAVLDelete(t *testing.T) {
t.Run("LLRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
if tree.Delete(5) {
t.Errorf("There is no node, whose value is 5")
}
tree.Push(5)
tree.Push(4)
tree.Push(3)
tree.Push(2)
if !tree.Delete(5) {
t.Errorf("There is a node, whose value is 5")
}
if tree.Delete(50) {
t.Errorf("There is no node, whose value is 50")
}
root := tree.Root
if root.Key() != 3 {
t.Errorf("Root should have value = 3")
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2")
}
if root.Left().Key() != 2 {
t.Errorf("Left child should have value = 2")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 4 {
t.Errorf("Right child should have value = 5")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("LRRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(10)
tree.Push(8)
tree.Push(8)
tree.Push(6)
tree.Push(7)
if !tree.Delete(10) {
t.Errorf("There is a node, whose value is 10")
}
if tree.Delete(5) {
t.Errorf("There is no node, whose value is 5")
}
root := tree.Root
if root.Key() != 7 {
t.Errorf("Root should have value = 7")
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2")
}
if root.Left().Key() != 6 {
t.Errorf("Left child should have value = 6")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 8 {
t.Errorf("Right child should have value = 8")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("RRRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(2)
tree.Push(3)
tree.Push(3)
tree.Push(4)
tree.Push(5)
if !tree.Delete(2) {
t.Errorf("There is a node, whose value is 2")
}
if tree.Delete(15) {
t.Errorf("There is no node, whose value is 15")
}
root := tree.Root
if root.Key() != 4 {
t.Errorf("Root should have value = 4")
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2")
}
if root.Left().Key() != 3 {
t.Errorf("Left child should have value = 3")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 5 {
t.Errorf("Right child should have value = 5")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("RLRotation-Test", func(t *testing.T) {
tree := bt.NewAVL[int]()
tree.Push(7)
tree.Push(6)
tree.Push(6)
tree.Push(9)
tree.Push(8)
tree.Delete(6)
root := tree.Root
if root.Key() != 8 {
t.Errorf("Root should have value = 8")
}
if root.Height() != 2 {
t.Errorf("Height of Root should be = 2")
}
if root.Left().Key() != 7 {
t.Errorf("Left child should have value = 7")
}
if root.Left().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Left child should be 1")
}
if root.Right().Key() != 9 {
t.Errorf("Right child should have value = 9")
}
if root.Right().(*bt.AVLNode[int]).Height() != 1 {
t.Errorf("Height of Right should be 1")
}
})
t.Run("Random Test", func(t *testing.T) {
nums := []int{100, 500, 1000, 10_000}
for _, n := range nums {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
tree := bt.NewAVL[int]()
nums := rnd.Perm(n)
tree.Push(nums...)
rets := tree.InOrder()
if !sort.IntsAreSorted(rets) {
t.Error("Error with Push")
}
if res, ok := tree.Min(); !ok || res != rets[0] {
t.Errorf("Error with Min, get %d, want: %d", res, rets[0])
}
if res, ok := tree.Max(); !ok || res != rets[n-1] {
t.Errorf("Error with Max, get %d, want: %d", res, rets[n-1])
}
for i := 0; i < n-1; i++ {
if ret, ok := tree.Successor(rets[0]); ret != rets[1] || !ok {
t.Error("Error with Successor")
}
if ret, ok := tree.Predecessor(rets[1]); ret != rets[0] || !ok {
t.Error("Error with Predecessor")
}
ok := tree.Delete(nums[i])
rets = tree.InOrder()
if !ok || !sort.IntsAreSorted(rets) {
t.Errorf("Error With Delete")
}
}
}
})
}
================================================
FILE: structure/tree/bstree.go
================================================
// Binary search tree.
//
// For more details check out those links below here:
// Wikipedia article: https://en.wikipedia.org/wiki/Binary_search_tree
// see bstree.go
package tree
import "github.com/TheAlgorithms/Go/constraints"
// Verify Interface Compliance
var _ Node[int] = &BSNode[int]{}
// BSNode represents a single node in the BinarySearch.
type BSNode[T constraints.Ordered] struct {
key T
parent *BSNode[T]
left *BSNode[T]
right *BSNode[T]
}
func (n *BSNode[T]) Key() T {
return n.key
}
func (n *BSNode[T]) Parent() Node[T] {
return n.parent
}
func (n *BSNode[T]) Left() Node[T] {
return n.left
}
func (n *BSNode[T]) Right() Node[T] {
return n.right
}
// BinarySearch represents a Binary-Search tree.
// By default, _NIL = nil.
type BinarySearch[T constraints.Ordered] struct {
Root *BSNode[T]
_NIL *BSNode[T] // a sentinel value for nil
}
// NewBinarySearch creates a novel Binary-Search tree
func NewBinarySearch[T constraints.Ordered]() *BinarySearch[T] {
return &BinarySearch[T]{
Root: nil,
_NIL: nil,
}
}
// Empty determines the Binary-Search tree is empty
func (t *BinarySearch[T]) Empty() bool {
return t.Root == t._NIL
}
// Push a chain of Node's into the BinarySearch
func (t *BinarySearch[T]) Push(keys ...T) {
for _, key := range keys {
t.pushHelper(t.Root, key)
}
}
// Delete removes the node of val
func (t *BinarySearch[T]) Delete(val T) bool {
node, ok := t.Get(val)
if !ok {
return false
}
t.deleteHelper(node.(*BSNode[T]))
return true
}
// Get a Node from the Binary-Search Tree
func (t *BinarySearch[T]) Get(key T) (Node[T], bool) {
return searchTreeHelper[T](t.Root, t._NIL, key)
}
// Has Determines the tree has the node of Key
func (t *BinarySearch[T]) Has(key T) bool {
_, ok := searchTreeHelper[T](t.Root, t._NIL, key)
return ok
}
// PreOrder Traverses the tree in the following order Root --> Left --> Right
func (t *BinarySearch[T]) PreOrder() []T {
traversal := make([]T, 0)
preOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// InOrder Traverses the tree in the following order Left --> Root --> Right
func (t *BinarySearch[T]) InOrder() []T {
return inOrderHelper[T](t.Root, t._NIL)
}
// PostOrder traverses the tree in the following order Left --> Right --> Root
func (t *BinarySearch[T]) PostOrder() []T {
traversal := make([]T, 0)
postOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// LevelOrder returns the level order traversal of the tree
func (t *BinarySearch[T]) LevelOrder() []T {
traversal := make([]T, 0)
levelOrderHelper[T](t.Root, t._NIL, &traversal)
return traversal
}
// AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.
func (t *BinarySearch[T]) AccessNodesByLayer() [][]T {
return accessNodeByLayerHelper[T](t.Root, t._NIL)
}
// Depth returns the calculated depth of a binary search tree
func (t *BinarySearch[T]) Depth() int {
return calculateDepth[T](t.Root, t._NIL, 0)
}
// Max returns the Max value of the tree
func (t *BinarySearch[T]) Max() (T, bool) {
ret := maximum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Min returns the Min value of the tree
func (t *BinarySearch[T]) Min() (T, bool) {
ret := minimum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Predecessor returns the Predecessor of the node of Key
// if there is no predecessor, return default value of type T and false
// otherwise return the Key of predecessor and true
func (t *BinarySearch[T]) Predecessor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return predecessorHelper[T](node, t._NIL)
}
// Successor returns the Successor of the node of Key
// if there is no successor, return default value of type T and false
// otherwise return the Key of successor and true
func (t *BinarySearch[T]) Successor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return successorHelper[T](node, t._NIL)
}
func (t *BinarySearch[T]) pushHelper(x *BSNode[T], val T) {
y := t._NIL
for x != t._NIL {
y = x
switch {
case val < x.Key():
x = x.left
case val > x.Key():
x = x.right
default:
return
}
}
z := &BSNode[T]{
key: val,
left: t._NIL,
right: t._NIL,
parent: y,
}
if y == t._NIL {
t.Root = z
} else if val < y.key {
y.left = z
} else {
y.right = z
}
}
func (t *BinarySearch[T]) deleteHelper(z *BSNode[T]) {
switch {
case z.left == t._NIL:
t.transplant(z, z.right)
case z.right == t._NIL:
t.transplant(z, z.left)
default:
y := minimum[T](z.right, t._NIL).(*BSNode[T])
if y.parent != z {
t.transplant(y, y.right)
y.right = z.right
y.right.parent = y
}
t.transplant(z, y)
y.left = z.left
y.left.parent = y
}
}
func (t *BinarySearch[T]) transplant(u, v *BSNode[T]) {
switch {
case u.parent == t._NIL:
t.Root = v
case u == u.parent.left:
u.parent.left = v
default:
u.parent.right = v
}
if v != t._NIL {
v.parent = u.parent
}
}
================================================
FILE: structure/tree/bstree_test.go
================================================
package tree_test
import (
"math/rand"
"sort"
"testing"
"time"
bt "github.com/TheAlgorithms/Go/structure/tree"
)
func TestPush(t *testing.T) {
bst := bt.NewBinarySearch[int]()
bst.Push(90)
bst.Push(80)
bst.Push(100)
if bst.Root.Key() != 90 {
t.Errorf("Root should have value = 90")
}
if bst.Root.Left().Key() != 80 {
t.Errorf("Left child should have value = 80")
}
if bst.Root.Right().Key() != 100 {
t.Errorf("Right child should have value = 100")
}
if bst.Depth() != 2 {
t.Errorf("tree should have depth = 1")
}
}
func TestDelete(t *testing.T) {
t.Run("Delete a node with no child", func(t *testing.T) {
bst := bt.NewBinarySearch[int]()
bst.Push(90)
bst.Push(80)
bst.Push(80)
bst.Push(100)
if !bst.Delete(100) {
t.Errorf("There is a node, whose value is 100")
}
if bst.Delete(105) {
t.Errorf("There is no node, whose value is 105")
}
root := bst.Root
if root.Key() != 90 {
t.Errorf("Root should have value = 90")
}
if root.Left().Key() != 80 {
t.Errorf("Left child should have value = 80")
}
if root.Right().(*bt.BSNode[int]) != nil {
t.Errorf("Right child should have value = nil")
}
if bst.Depth() != 2 {
t.Errorf("Depth should have value = 2")
}
bst.Delete(80)
if root.Key() != 90 {
t.Errorf("Root should have value = 90")
}
if root.Left().(*bt.BSNode[int]) != nil {
t.Errorf("Left child should have value = nil")
}
if bst.Depth() != 1 {
t.Errorf("Depth should have value = 1")
}
})
t.Run("Delete a node with one child", func(t *testing.T) {
bst := bt.NewBinarySearch[int]()
bst.Push(90)
bst.Push(80)
bst.Push(100)
bst.Push(70)
if bst.Delete(102) {
t.Errorf("There is no node, whose value is 102")
}
if !bst.Delete(80) {
t.Errorf("There is a node, whose value is 80")
}
root := bst.Root
if root.Key() != 90 {
t.Errorf("Root should have value = 90")
}
if root.Right().Key() != 100 {
t.Errorf("Right child should have value = 100")
}
if root.Left().Key() != 70 {
t.Errorf("Left child should have value = 70")
}
if bst.Depth() != 2 {
t.Errorf("Depth should have value = 2")
}
})
t.Run("Delete a node with two children", func(t *testing.T) {
bst := bt.NewBinarySearch[int]()
bst.Push(90)
bst.Push(80)
bst.Push(100)
bst.Push(70)
bst.Push(85)
if !bst.Delete(80) {
t.Errorf("There is a node, whose value is 80")
}
if bst.Delete(102) {
t.Errorf("There is no node, whose value is 102")
}
root := bst.Root
if root.Key() != 90 {
t.Errorf("Root should have value = 90")
}
if root.Left().Key() != 85 {
t.Errorf("Left child should have value = 85")
}
if root.Right().Key() != 100 {
t.Errorf("Right child should have value = 100")
}
if bst.Depth() != 3 {
t.Errorf("Depth should have value = 3")
}
})
t.Run("Random Test", func(t *testing.T) {
tests := []int{100, 500, 1000, 10_000}
for _, n := range tests {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
tree := bt.NewBinarySearch[int]()
nums := rnd.Perm(n)
tree.Push(nums...)
rets := tree.InOrder()
if !sort.IntsAreSorted(rets) {
t.Error("Error with Push")
}
if res, ok := tree.Min(); !ok || res != rets[0] {
t.Errorf("Error with Min, get %d, want: %d", res, rets[0])
}
if res, ok := tree.Max(); !ok || res != rets[n-1] {
t.Errorf("Error with Max, get %d, want: %d", res, rets[n-1])
}
for i := 0; i < n-1; i++ {
if ret, ok := tree.Successor(rets[0]); ret != rets[1] || !ok {
t.Error("Error with Successor")
}
if ret, ok := tree.Predecessor(rets[1]); ret != rets[0] || !ok {
t.Error("Error with Predecessor")
}
ok := tree.Delete(nums[i])
rets = tree.InOrder()
if !ok || !sort.IntsAreSorted(rets) {
t.Errorf("Error With Delete")
}
}
}
})
}
================================================
FILE: structure/tree/btree.go
================================================
// B-tree is a self balancing tree that promotes data locality.
// For more details, see https://en.wikipedia.org/wiki/B-tree
package tree
import "github.com/TheAlgorithms/Go/constraints"
type BTreeNode[T constraints.Ordered] struct {
keys []T
children []*BTreeNode[T]
numKeys int
isLeaf bool
}
type BTree[T constraints.Ordered] struct {
root *BTreeNode[T]
maxKeys int
}
func minKeys(maxKeys int) int {
return (maxKeys - 1) / 2
}
func NewBTreeNode[T constraints.Ordered](maxKeys int, isLeaf bool) *BTreeNode[T] {
if maxKeys <= 0 {
panic("BTree maxKeys cannot be zero")
}
return &BTreeNode[T]{
keys: make([]T, maxKeys),
children: make([]*BTreeNode[T], maxKeys+1),
isLeaf: isLeaf,
}
}
func NewBTree[T constraints.Ordered](maxKeys int) *BTree[T] {
if maxKeys <= 2 {
panic("Must be >= 3 keys")
}
return &BTree[T]{
root: nil,
maxKeys: maxKeys,
}
}
func (node *BTreeNode[T]) Verify(tree *BTree[T]) {
minKeys := minKeys(tree.maxKeys)
if node != tree.root && node.numKeys < minKeys {
panic("node has too few keys")
} else if node.numKeys > tree.maxKeys {
panic("node has too many keys")
}
}
func (node *BTreeNode[T]) IsFull(maxKeys int) bool {
return node.numKeys == maxKeys
}
func (node *BTreeNode[T]) Search(key T) bool {
i := 0
for ; i < node.numKeys; i++ {
if key == node.keys[i] {
return true
}
if key < node.keys[i] {
break
}
}
if node.isLeaf {
return false
}
return node.children[i].Search(key)
}
func (tree *BTree[T]) Search(key T) bool {
if tree.root == nil {
return false
}
return tree.root.Search(key)
}
func (node *BTreeNode[T]) InsertKeyChild(key T, child *BTreeNode[T]) {
i := node.numKeys
node.children[i+1] = node.children[i]
for ; i > 0; i-- {
if key > node.keys[i-1] {
node.keys[i] = key
node.children[i] = child
break
}
node.keys[i] = node.keys[i-1]
node.children[i] = node.children[i-1]
}
if i == 0 {
node.keys[0] = key
node.children[0] = child
}
node.numKeys++
}
func (node *BTreeNode[T]) Append(key T, child *BTreeNode[T]) {
node.keys[node.numKeys] = key
node.children[node.numKeys+1] = child
node.numKeys++
}
// Add all of other's keys starting from idx and children starting from idx + 1
func (node *BTreeNode[T]) Concat(other *BTreeNode[T], idx int) {
for i := 0; i < other.numKeys-idx; i++ {
node.keys[node.numKeys+i] = other.keys[i+idx]
node.children[node.numKeys+i+1] = other.children[i+idx+1]
}
node.numKeys += other.numKeys - idx
}
// Transform:
//
// A B
// |
//
// a b c d
//
// Into:
//
// A c B
// / \
//
// a b d
func (parent *BTreeNode[T]) Split(idx int, maxKeys int) {
child := parent.children[idx]
midKeyIndex := maxKeys / 2
rightChild := NewBTreeNode[T](maxKeys, child.isLeaf)
rightChild.Concat(child, midKeyIndex+1)
rightChild.children[0] = child.children[midKeyIndex+1]
// Reuse child as the left node
child.numKeys = midKeyIndex
// Insert the child's mid index to the parent
for i := parent.numKeys; i > idx; i-- {
parent.keys[i] = parent.keys[i-1]
parent.children[i+1] = parent.children[i]
}
parent.keys[idx] = child.keys[midKeyIndex]
parent.children[idx] = child
parent.children[idx+1] = rightChild
parent.numKeys += 1
}
func (node *BTreeNode[T]) InsertNonFull(tree *BTree[T], key T) {
node.Verify(tree)
if node.IsFull(tree.maxKeys) {
panic("Called InsertNonFull() with a full node")
}
if node.isLeaf {
// Node is a leaf. Directly insert the key.
node.InsertKeyChild(key, nil)
return
}
// Find the child node to insert into
i := 0
for ; i < node.numKeys; i++ {
if key < node.keys[i] {
break
}
}
if node.children[i].IsFull(tree.maxKeys) {
node.Split(i, tree.maxKeys)
if key > node.keys[i] {
i++
}
}
node.children[i].InsertNonFull(tree, key)
}
func (tree *BTree[T]) Insert(key T) {
if tree.root == nil {
tree.root = NewBTreeNode[T](tree.maxKeys, true)
tree.root.keys[0] = key
tree.root.numKeys = 1
return
}
if tree.root.IsFull(tree.maxKeys) {
newRoot := NewBTreeNode[T](tree.maxKeys, false)
newRoot.numKeys = 0
newRoot.children[0] = tree.root
newRoot.Split(0, tree.maxKeys)
tree.root = newRoot
}
tree.root.InsertNonFull(tree, key)
}
func (node *BTreeNode[T]) DeleteIthKey(i int) {
if i >= node.numKeys {
panic("deleting out of bounds key")
}
for j := i; j < node.numKeys-1; j++ {
node.keys[j] = node.keys[j+1]
node.children[j+1] = node.children[j+2]
}
node.numKeys--
}
// Transform:
//
// A B C
// / \
// a b
//
// Into:
//
// A C
// |
//
// a B c
func (node *BTreeNode[T]) Merge(idx int) {
if node.isLeaf {
panic("cannot merge when leaf node is parent")
}
left := node.children[idx]
right := node.children[idx+1]
left.Append(node.keys[idx], right.children[0])
left.Concat(right, 0)
node.DeleteIthKey(idx)
}
func (node *BTreeNode[T]) Min() T {
if node.isLeaf {
return node.keys[0]
}
return node.children[0].Min()
}
func (node *BTreeNode[T]) Max() T {
if node.isLeaf {
return node.keys[node.numKeys-1]
}
return node.children[node.numKeys].Max()
}
func (node *BTreeNode[T]) Delete(tree *BTree[T], key T) {
node.Verify(tree)
if node.isLeaf {
// Case 1: Node is a leaf. Directly delete the key.
for i := 0; i < node.numKeys; i++ {
if key == node.keys[i] {
node.DeleteIthKey(i)
return
}
}
return
}
minKeys := minKeys(tree.maxKeys)
i := 0
for ; i < node.numKeys; i++ {
if key == node.keys[i] {
// Case 2: key exists in a non-leaf node
left := node.children[i]
right := node.children[i+1]
if left.numKeys > minKeys {
// Replace the key we want to delete with the max key from the left
// subtree. Then delete that key from the left subtree.
// A B C
// /
// a b c
//
// If we want to delete `B`, then replace `B` with `c`, and delete `c` in the subtree.
// A c C
// /
// a b
replacementKey := left.Max()
node.keys[i] = replacementKey
left.Delete(tree, replacementKey)
} else if right.numKeys > minKeys {
// Replace the key we want to delete with the min key from the right
// subtree. Then delete that key in the right subtree. Mirrors the
// transformation above for replacing from the left subtree.
replacementKey := right.Min()
node.keys[i] = replacementKey
right.Delete(tree, replacementKey)
} else {
// Both left and right subtrees have the minimum number of keys. Merge
// the left tree, the deleted key, and the right tree together into the
// left tree. Then recursively delete the key in the left tree.
if left.numKeys != minKeys || right.numKeys != minKeys {
panic("nodes should not have less than the minimum number of keys")
}
node.Merge(i)
left.Delete(tree, key)
}
return
}
if key < node.keys[i] {
break
}
}
// Case 3: key may exist in a child node.
child := node.children[i]
if child.numKeys == minKeys {
// Before we recurse into the child node, make sure it has more than
// the minimum number of keys.
if i > 0 && node.children[i-1].numKeys > minKeys {
// Take a key from the left sibling
// Transform:
// A B C
// / \
// a b c
//
// Into:
// A b C
// / \
// a B c
left := node.children[i-1]
child.InsertKeyChild(node.keys[i-1], left.children[left.numKeys])
node.keys[i-1] = left.keys[left.numKeys-1]
left.numKeys--
} else if i < node.numKeys && node.children[i+1].numKeys > minKeys {
// Take a key from the right sibling. Mirrors the transformation above for taking a key from the left sibling.
right := node.children[i+1]
child.Append(node.keys[i], right.children[0])
node.keys[i] = right.keys[0]
right.children[0] = right.children[1]
right.DeleteIthKey(0)
} else {
if i == 0 {
// Merge with right sibling
node.Merge(i)
} else {
// Merge with left sibling
node.Merge(i - 1)
child = node.children[i-1]
}
}
}
if child.numKeys == minKeys {
panic("cannot delete key from node with minimum number of keys")
}
child.Delete(tree, key)
}
func (tree *BTree[T]) Delete(key T) {
if tree.root == nil {
return
}
tree.root.Delete(tree, key)
if tree.root.numKeys == 0 {
tree.root = tree.root.children[0]
}
}
================================================
FILE: structure/tree/btree_test.go
================================================
package tree_test
import (
bt "github.com/TheAlgorithms/Go/structure/tree"
"math/rand"
"testing"
)
func TestBTreeIncreasing(t *testing.T) {
maxKeysCases := []int{4, 16}
sizes := []int{100, 0xBA5, 0xF00}
for _, maxKeys := range maxKeysCases {
for _, size := range sizes {
tree := bt.NewBTree[int](maxKeys)
if tree.Search(0) {
t.Errorf("Tree expected to contain 0")
}
for i := 0; i < size; i++ {
tree.Insert(i)
}
for i := 0; i < size; i++ {
if !tree.Search(i) {
t.Errorf("Tree expected to contain %d", i)
}
}
if tree.Search(size + 1) {
t.Errorf("Tree not expected to contain %d", size+1)
}
for i := 0; i < size; i += 5 {
tree.Delete(i)
}
for i := 0; i < size; i++ {
hasKey := tree.Search(i)
if i%5 == 0 && hasKey {
t.Errorf("Tree not expected to contain %d", i)
} else if i%5 != 0 && !hasKey {
t.Errorf("Tree expected to contain %d", i)
}
}
}
}
}
func TestBTreeDecreasing(t *testing.T) {
maxKeysCases := []int{4, 16}
sizes := []int{100, 1000}
for _, maxKeys := range maxKeysCases {
for _, size := range sizes {
tree := bt.NewBTree[int](maxKeys)
if tree.Search(0) {
t.Errorf("Tree expected to contain 0")
}
for i := size - 1; i >= 0; i-- {
tree.Insert(i)
}
for i := 0; i < size; i++ {
if !tree.Search(i) {
t.Errorf("Tree expected to contain %d", i)
}
}
if tree.Search(size + 1) {
t.Errorf("Tree not expected to contain %d", size+1)
}
for i := 0; i < size; i += 5 {
tree.Delete(i)
}
for i := 0; i < size; i++ {
hasKey := tree.Search(i)
if i%5 == 0 && hasKey {
t.Errorf("Tree not expected to contain %d", i)
} else if i%5 != 0 && !hasKey {
t.Errorf("Tree expected to contain %d", i)
}
}
}
}
}
func TestBTreeRandom(t *testing.T) {
maxKeysCases := []int{4, 16}
sizes := []int{100, 0xBA5, 0xF00}
for _, maxKeys := range maxKeysCases {
for _, size := range sizes {
rnd := rand.New(rand.NewSource(0))
tree := bt.NewBTree[int](maxKeys)
nums := rnd.Perm(size)
if tree.Search(0) {
t.Errorf("Tree expected to contain 0")
}
for i := 0; i < size; i++ {
tree.Insert(nums[i])
}
for i := 0; i < size; i++ {
if !tree.Search(nums[i]) {
t.Errorf("Tree expected to contain %d", nums[i])
}
}
for i := 0; i < size; i += 5 {
tree.Delete(nums[i])
}
for i := 0; i < size; i++ {
hasKey := tree.Search(nums[i])
if i%5 == 0 && hasKey {
t.Errorf("Tree not expected to contain %d", i)
} else if i%5 != 0 && !hasKey {
t.Errorf("Tree expected to contain %d", i)
}
}
}
}
}
func TestBTreeDeleteEverything(t *testing.T) {
tree := bt.NewBTree[int](4)
size := 128
for i := 0; i < size; i++ {
tree.Insert(i)
}
for i := 0; i < size; i++ {
tree.Delete(i)
}
tree.Delete(-1)
tree.Delete(1000)
for i := 0; i < size; i++ {
if tree.Search(i) {
t.Errorf("Tree not expected to contain %d", i)
}
}
}
================================================
FILE: structure/tree/example_test.go
================================================
package tree_test
import (
"fmt"
"github.com/TheAlgorithms/Go/constraints"
"testing"
bt "github.com/TheAlgorithms/Go/structure/tree"
)
type TestTree[T constraints.Ordered] interface {
Push(...T)
Delete(T) bool
Get(T) (bt.Node[T], bool)
Empty() bool
Has(T) bool
Depth() int
Max() (T, bool)
Min() (T, bool)
Predecessor(T) (T, bool)
Successor(T) (T, bool)
PreOrder() []T
InOrder() []T
PostOrder() []T
LevelOrder() []T
AccessNodesByLayer() [][]T
}
// BinarySearch, AVL and RB have completed the `TestTree` interface.
var (
_ TestTree[int] = (*bt.BinarySearch[int])(nil)
_ TestTree[int] = (*bt.AVL[int])(nil)
_ TestTree[int] = (*bt.RB[int])(nil)
)
var tree TestTree[int]
func TestBinarySearch(t *testing.T) {
tree = bt.NewBinarySearch[int]()
if tree.Empty() {
t.Log("Binary Search Tree is empty now.")
}
tree.Push(1, 4, 10)
tree.Push(-8)
nums := []int{87, 18, 10, -34}
tree.Push(nums...)
tree.Push(4) // duplicate key, dismiss it
if tree.Has(4) {
t.Logf("There is a node of 4")
}
if n, ok := tree.Get(10); ok {
t.Logf("node of 10: %T", n)
}
if ret, ok := tree.Min(); ok {
t.Logf("tree.Min() = %v", ret)
}
if ret, ok := tree.Max(); ok {
t.Logf("tree.Max() = %v", ret)
}
if ret, ok := tree.Predecessor(1); ok {
t.Logf("tree.Preducessor(1) = %v", ret)
}
if ret, ok := tree.Successor(18); ok {
t.Logf("tree.Successor(18) = %v", ret)
}
fmt.Println(tree.InOrder())
fmt.Println(tree.AccessNodesByLayer())
tree.Delete(18)
fmt.Println("Delete 18")
fmt.Println(tree.InOrder())
}
func TestAVL(t *testing.T) {
tree = bt.NewAVL[int]()
if tree.Empty() {
t.Log("AVL Tree is empty now.")
}
tree.Push(1, 4, 10)
tree.Push(-8)
nums := []int{87, 18, 10, -34}
tree.Push(nums...)
tree.Push(4) // duplicate key, dismiss it
if tree.Has(4) {
t.Logf("There is a node of 4")
}
if n, ok := tree.Get(10); ok {
t.Logf("node of 10: %T", n)
}
if ret, ok := tree.Min(); ok {
t.Logf("tree.Min() = %v", ret)
}
if ret, ok := tree.Max(); ok {
t.Logf("tree.Max() = %v", ret)
}
if ret, ok := tree.Predecessor(1); ok {
t.Logf("tree.Preducessor(1) = %v", ret)
}
if ret, ok := tree.Successor(18); ok {
t.Logf("tree.Successor(18) = %v", ret)
}
fmt.Println(tree.InOrder())
fmt.Println(tree.AccessNodesByLayer())
tree.Delete(18)
fmt.Println("Delete 18")
fmt.Println(tree.InOrder())
}
func TestRB(t *testing.T) {
tree = bt.NewRB[int]()
if tree.Empty() {
t.Log("RB Tree is empty now.")
}
tree.Push(1, 4, 10)
tree.Push(-8)
nums := []int{87, 18, 10, -34}
tree.Push(nums...)
tree.Push(4) // duplicate key, dismiss it
if tree.Has(4) {
t.Logf("There is a node of 4")
}
if n, ok := tree.Get(10); ok {
t.Logf("node of 10: %T", n)
}
if ret, ok := tree.Min(); ok {
t.Logf("tree.Min() = %v", ret)
}
if ret, ok := tree.Max(); ok {
t.Logf("tree.Max() = %v", ret)
}
if ret, ok := tree.Predecessor(1); ok {
t.Logf("tree.Preducessor(1) = %v", ret)
}
if ret, ok := tree.Successor(18); ok {
t.Logf("tree.Successor(18) = %v", ret)
}
fmt.Println(tree.InOrder())
fmt.Println(tree.AccessNodesByLayer())
}
================================================
FILE: structure/tree/rbtree.go
================================================
// Red-Black Tree is a kind of self-balancing binary search tree.
// Each node stores "color" ("red" or "black"), used to ensure that the tree remains balanced during insertions and deletions.
//
// For more details check out those links below here:
// Programiz article : https://www.programiz.com/dsa/red-black-tree
// Wikipedia article: https://en.wikipedia.org/wiki/Red_black_tree
// authors [guuzaa](https://github.com/guuzaa)
// see rbtree.go
package tree
import "github.com/TheAlgorithms/Go/constraints"
type Color byte
const (
Red Color = iota
Black
)
// Verify Interface Compliance
var _ Node[int] = &RBNode[int]{}
// RBNode represents a single node in the RB.
type RBNode[T constraints.Ordered] struct {
key T
parent *RBNode[T]
left *RBNode[T]
right *RBNode[T]
color Color
}
func (n *RBNode[T]) Key() T {
return n.key
}
func (n *RBNode[T]) Parent() Node[T] {
return n.parent
}
func (n *RBNode[T]) Left() Node[T] {
return n.left
}
func (n *RBNode[T]) Right() Node[T] {
return n.right
}
// RB represents a Red-Black tree.
// By default, _NIL = leaf, a dummy variable.
type RB[T constraints.Ordered] struct {
Root *RBNode[T]
_NIL *RBNode[T] // a sentinel value for nil
}
// NewRB creates a new Red-Black Tree
func NewRB[T constraints.Ordered]() *RB[T] {
leaf := &RBNode[T]{color: Black, left: nil, right: nil}
leaf.parent = leaf
return &RB[T]{
Root: leaf,
_NIL: leaf,
}
}
// Empty determines the Red-Black tree is empty
func (t *RB[T]) Empty() bool {
return t.Root == t._NIL
}
// Push a chain of Node's into the Red-Black Tree
func (t *RB[T]) Push(keys ...T) {
for _, key := range keys {
t.pushHelper(t.Root, key)
}
}
// Delete a node of Red-Black Tree
// Returns false if the node does not exist, otherwise returns true.
func (t *RB[T]) Delete(data T) bool {
return t.deleteHelper(t.Root, data)
}
// Get a Node from the Red-Black Tree
func (t *RB[T]) Get(key T) (Node[T], bool) {
return searchTreeHelper[T](t.Root, t._NIL, key)
}
// Has Determines the tree has the node of Key
func (t *RB[T]) Has(key T) bool {
_, ok := searchTreeHelper[T](t.Root, t._NIL, key)
return ok
}
// PreOrder Traverses the tree in the following order Root --> Left --> Right
func (t *RB[T]) PreOrder() []T {
traversal := make([]T, 0)
preOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// InOrder Traverses the tree in the following order Left --> Root --> Right
func (t *RB[T]) InOrder() []T {
return inOrderHelper[T](t.Root, t._NIL)
}
// PostOrder traverses the tree in the following order Left --> Right --> Root
func (t *RB[T]) PostOrder() []T {
traversal := make([]T, 0)
postOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// LevelOrder returns the level order traversal of the tree
func (t *RB[T]) LevelOrder() []T {
traversal := make([]T, 0)
levelOrderHelper[T](t.Root, t._NIL, &traversal)
return traversal
}
// AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.
func (t *RB[T]) AccessNodesByLayer() [][]T {
return accessNodeByLayerHelper[T](t.Root, t._NIL)
}
// Depth returns the calculated depth of a Red-Black tree
func (t *RB[T]) Depth() int {
return calculateDepth[T](t.Root, t._NIL, 0)
}
// Max returns the Max value of the tree
func (t *RB[T]) Max() (T, bool) {
ret := maximum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Min returns the Min value of the tree
func (t *RB[T]) Min() (T, bool) {
ret := minimum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Predecessor returns the Predecessor of the node of Key
// if there is no predecessor, return default value of type T and false
// otherwise return the Key of predecessor and true
func (t *RB[T]) Predecessor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return predecessorHelper[T](node, t._NIL)
}
// Successor returns the Successor of the node of Key
// if there is no successor, return default value of type T and false
// otherwise return the Key of successor and true
func (t *RB[T]) Successor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return successorHelper[T](node, t._NIL)
}
func (t *RB[T]) pushHelper(x *RBNode[T], key T) {
y := t._NIL
for x != t._NIL {
y = x
switch {
case key < x.Key():
x = x.left
case key > x.Key():
x = x.right
default:
return
}
}
node := &RBNode[T]{
key: key,
left: t._NIL,
right: t._NIL,
parent: y,
color: Red,
}
if y == t._NIL {
t.Root = node
} else if node.key < y.key {
y.left = node
} else {
y.right = node
}
if node.parent == t._NIL {
node.color = Black
return
}
if node.parent.parent == t._NIL {
return
}
t.pushFix(node)
}
func (t *RB[T]) leftRotate(x *RBNode[T]) {
y := x.right
x.right = y.left
if y.left != t._NIL {
y.left.parent = x
}
y.parent = x.parent
if x.parent == t._NIL {
t.Root = y
} else if x == x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
}
y.left = x
x.parent = y
}
func (t *RB[T]) rightRotate(x *RBNode[T]) {
y := x.left
x.left = y.right
if y.right != t._NIL {
y.right.parent = x
}
y.parent = x.parent
if x.parent == t._NIL {
t.Root = y
} else if x == y.parent.right {
y.parent.right = y
} else {
y.parent.left = y
}
y.right = x
x.parent = y
}
func (t *RB[T]) pushFix(k *RBNode[T]) {
for k.parent.color == Red {
if k.parent == k.parent.parent.right {
u := k.parent.parent.left
if u.color == Red {
u.color = Black
k.parent.color = Black
k.parent.parent.color = Red
k = k.parent.parent
} else {
if k == k.parent.left {
k = k.parent
t.rightRotate(k)
}
k.parent.color = Black
k.parent.parent.color = Red
t.leftRotate(k.parent.parent)
}
} else {
u := k.parent.parent.right
if u.color == Red {
u.color = Black
k.parent.color = Black
k.parent.parent.color = Red
k = k.parent.parent
} else {
if k == k.parent.right {
k = k.parent
t.leftRotate(k)
}
k.parent.color = Black
k.parent.parent.color = Red
t.rightRotate(k.parent.parent)
}
}
if k == t.Root {
break
}
}
t.Root.color = Black
}
func (t *RB[T]) deleteHelper(node *RBNode[T], key T) bool {
z := t._NIL
for node != t._NIL {
switch {
case node.key == key:
z = node
fallthrough
case node.key <= key:
node = node.right
case node.key > key:
node = node.left
}
}
if z == t._NIL {
return false
}
var x *RBNode[T]
y := z
yOriginColor := y.color
if z.left == t._NIL {
x = z.right
t.transplant(z, z.right)
} else if z.right == t._NIL {
x = z.left
t.transplant(z, z.left)
} else {
y = minimum[T](z.right, t._NIL).(*RBNode[T])
yOriginColor = y.color
x = y.right
if y.parent == z {
x.parent = y
} else {
t.transplant(y, y.right)
y.right = z.right
y.right.parent = y
}
t.transplant(z, y)
y.left = z.left
y.left.parent = y
y.color = z.color
}
if yOriginColor == Black {
t.deleteFix(x)
}
return true
}
func (t *RB[T]) deleteFix(x *RBNode[T]) {
var s *RBNode[T]
for x != t.Root && x.color == Black {
if x == x.parent.left {
s = x.parent.right
if s.color == Red {
s.color = Black
x.parent.color = Red
t.leftRotate(x.parent)
s = x.parent.right
}
if s.left.color == Black && s.right.color == Black {
s.color = Red
x = x.parent
} else {
if s.right.color == Black {
s.left.color = Black
s.color = Red
t.rightRotate(s)
s = x.parent.right
}
s.color = x.parent.color
x.parent.color = Black
s.right.color = Black
t.leftRotate(x.parent)
x = t.Root
}
} else {
s = x.parent.left
if s.color == Red {
s.color = Black
x.parent.color = Red
t.rightRotate(x.parent)
s = x.parent.left
}
if s.right.color == Black && s.left.color == Black {
s.color = Red
x = x.parent
} else {
if s.left.color == Black {
s.right.color = Black
s.color = Red
t.leftRotate(s)
s = x.parent.left
}
s.color = x.parent.color
x.parent.color = Black
s.left.color = Black
t.rightRotate(x.parent)
x = t.Root
}
}
}
x.color = Black
}
func (t *RB[T]) transplant(u, v *RBNode[T]) {
switch {
case u.parent == t._NIL:
t.Root = v
case u == u.parent.left:
u.parent.left = v
default:
u.parent.right = v
}
v.parent = u.parent
}
================================================
FILE: structure/tree/rbtree_test.go
================================================
package tree_test
import (
"math/rand"
"sort"
"testing"
"time"
bt "github.com/TheAlgorithms/Go/structure/tree"
)
func TestRBTreePush(t *testing.T) {
tree := bt.NewRB[int]()
ret := tree.InOrder()
if !sort.IntsAreSorted(ret) || len(ret) != 0 {
t.Errorf("Error with Push: %v", ret)
}
if r, ok := tree.Min(); ok {
t.Errorf("Error with Min: %v", r)
}
if r, ok := tree.Max(); ok {
t.Errorf("Error with Max: %v", r)
}
nums := []int{10, 8, 88, 888, 4, 1<<63 - 1, -(1 << 62), 188, -188, 4, 1 << 32}
tree.Push(nums...)
ret = tree.InOrder()
if !sort.IntsAreSorted(ret) {
t.Errorf("Error with Push: %v", ret)
}
if r, ok := tree.Min(); !ok || ret[0] != r {
t.Errorf("Error with Min: %v", r)
}
if r, ok := tree.Max(); !ok || ret[len(ret)-1] != r {
t.Errorf("Error with Max: %v", r)
}
}
func TestRBTreeDelete(t *testing.T) {
tree := bt.NewRB[int]()
var ok bool
nums := []int{10, 8, 88, 888, 4, 1<<63 - 1, -(1 << 62), 188, -188, 4, 88, 1 << 32}
tree.Push(nums...)
ok = tree.Delete(188)
if ret := tree.InOrder(); !ok || !sort.IntsAreSorted(ret) {
t.Errorf("Error with Delete: %v", ret)
}
ok = tree.Delete(188)
if ret := tree.InOrder(); ok || !sort.IntsAreSorted(ret) {
t.Errorf("Error with Delete: %v", ret)
}
ok = tree.Delete(1<<63 - 1)
if ret := tree.InOrder(); !ok || !sort.IntsAreSorted(ret) {
t.Errorf("Error with Delete: %v", ret)
}
ok = tree.Delete(4)
if ret := tree.InOrder(); !ok || !sort.IntsAreSorted(ret) {
t.Errorf("Error with Delete: %v", ret)
}
if ret, ok := tree.Max(); !ok || ret != (1<<32) {
t.Errorf("Error with Delete, max: %v, want: %v", ret, (1 << 32))
}
if ret, ok := tree.Min(); !ok || ret != -(1<<62) {
t.Errorf("Error with Delete, min: %v, want: %v", ret, (1 << 32))
}
}
func TestRBTree(t *testing.T) {
testcases := []int{100, 200, 1000, 10000}
for _, n := range testcases {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
tree := bt.NewRB[int]()
nums := rnd.Perm(n)
tree.Push(nums...)
rets := tree.InOrder()
if !sort.IntsAreSorted(rets) {
t.Error("Error with Push")
}
if res, ok := tree.Min(); !ok || res != rets[0] {
t.Errorf("Error with Min, get %d, want: %d", res, rets[0])
}
if res, ok := tree.Max(); !ok || res != rets[n-1] {
t.Errorf("Error with Max, get %d, want: %d", res, rets[n-1])
}
for i := 0; i < n-1; i++ {
if ret, ok := tree.Successor(rets[0]); ret != rets[1] || !ok {
t.Error("Error with Successor")
}
if ret, ok := tree.Predecessor(rets[1]); ret != rets[0] || !ok {
t.Error("Error with Predecessor")
}
ok := tree.Delete(nums[i])
rets = tree.InOrder()
if !ok || !sort.IntsAreSorted(rets) {
t.Errorf("Error With Delete")
}
}
}
}
func TestRBTreeString(t *testing.T) {
tree := bt.NewRB[string]()
if tree.Has("Golang") {
t.Errorf("Error with Has when T is string.")
}
strs := []string{"Hello", "World", "Golang", "Python", "Rust", "C", "JavaScript", "Haskell", "Pascal", "ZZ"}
for _, str := range strs {
tree.Push(str)
}
if !tree.Has("Golang") {
t.Errorf("Error with Has when T is string.")
}
if tree.Has("Pasc") {
t.Errorf("Error with Has when T is string.")
}
if ok := tree.Delete("Hello"); !ok {
t.Errorf("Error with Delete when T is string.")
}
if ok := tree.Delete("Pasc"); ok {
t.Errorf("Error with Delete when T is string.")
}
if tree.Has("Hello") {
t.Errorf("Error with Has when T is string.")
}
ret := tree.InOrder()
if !sort.StringsAreSorted(ret) {
t.Errorf("Error with Push when T is string")
}
if ret, ok := tree.Min(); !ok || ret != "C" {
t.Errorf("Error with Min when T is string")
}
if ret, ok := tree.Max(); !ok || ret != "ZZ" {
t.Errorf("Error with Max when T is string")
}
}
================================================
FILE: structure/tree/tree.go
================================================
// Binary-Search tree is the tree, with the key of each internal node
// being greater than keys in the respective node's left subtree
// and less than the ones in its right subtree.
// For more details check out those links below here:
// Wikipedia article: https://en.wikipedia.org/wiki/Binary_search_tree
// authors [guuzaa](https://github.com/guuzaa)
package tree
import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/max"
)
type Node[T constraints.Ordered] interface {
Key() T
Parent() Node[T]
Left() Node[T]
Right() Node[T]
}
// The following is a collection of helper functions for BinarySearch, AVL and RB.
func accessNodeByLayerHelper[T constraints.Ordered](root, nilNode Node[T]) [][]T {
if root == nilNode {
return [][]T{}
}
var q []Node[T]
var n Node[T]
var idx = 0
q = append(q, root)
var res [][]T
for len(q) != 0 {
res = append(res, []T{})
qLen := len(q)
for i := 0; i < qLen; i++ {
n, q = q[0], q[1:]
res[idx] = append(res[idx], n.Key())
if n.Left() != nilNode {
q = append(q, n.Left())
}
if n.Right() != nilNode {
q = append(q, n.Right())
}
}
idx++
}
return res
}
func searchTreeHelper[T constraints.Ordered](node, nilNode Node[T], key T) (Node[T], bool) {
if node == nilNode {
return node, false
}
if key == node.Key() {
return node, true
}
if key < node.Key() {
return searchTreeHelper(node.Left(), nilNode, key)
}
return searchTreeHelper(node.Right(), nilNode, key)
}
func inOrderHelper[T constraints.Ordered](node, nilNode Node[T]) []T {
var stack []Node[T]
var ret []T
for node != nilNode || len(stack) > 0 {
for node != nilNode {
stack = append(stack, node)
node = node.Left()
}
node = stack[len(stack)-1]
stack = stack[:len(stack)-1]
ret = append(ret, node.Key())
node = node.Right()
}
return ret
}
func preOrderRecursive[T constraints.Ordered](n, nilNode Node[T], traversal *[]T) {
if n == nilNode {
return
}
*traversal = append(*traversal, n.Key())
preOrderRecursive(n.Left(), nilNode, traversal)
preOrderRecursive(n.Right(), nilNode, traversal)
}
func postOrderRecursive[T constraints.Ordered](n, nilNode Node[T], traversal *[]T) {
if n == nilNode {
return
}
postOrderRecursive(n.Left(), nilNode, traversal)
postOrderRecursive(n.Right(), nilNode, traversal)
*traversal = append(*traversal, n.Key())
}
func calculateDepth[T constraints.Ordered](n, nilNode Node[T], depth int) int {
if n == nilNode {
return depth
}
return max.Int(calculateDepth(n.Left(), nilNode, depth+1), calculateDepth(n.Right(), nilNode, depth+1))
}
func minimum[T constraints.Ordered](node, nilNode Node[T]) Node[T] {
if node == nilNode {
return node
}
for node.Left() != nilNode {
node = node.Left()
}
return node
}
func maximum[T constraints.Ordered](node, nilNode Node[T]) Node[T] {
if node == nilNode {
return node
}
for node.Right() != nilNode {
node = node.Right()
}
return node
}
func levelOrderHelper[T constraints.Ordered](root, nilNode Node[T], traversal *[]T) {
var q []Node[T] // queue
var tmp Node[T]
q = append(q, root)
for len(q) != 0 {
tmp, q = q[0], q[1:]
*traversal = append(*traversal, tmp.Key())
if tmp.Left() != nilNode {
q = append(q, tmp.Left())
}
if tmp.Right() != nilNode {
q = append(q, tmp.Right())
}
}
}
func predecessorHelper[T constraints.Ordered](node, nilNode Node[T]) (T, bool) {
if node.Left() != nilNode {
return maximum(node.Left(), nilNode).Key(), true
}
p := node.Parent()
for p != nilNode && node == p.Left() {
node = p
p = p.Parent()
}
if p == nilNode {
var dft T
return dft, false
}
return p.Key(), true
}
func successorHelper[T constraints.Ordered](node, nilNode Node[T]) (T, bool) {
if node.Right() != nilNode {
return minimum(node.Right(), nilNode).Key(), true
}
p := node.Parent()
for p != nilNode && node == p.Right() {
node = p
p = p.Parent()
}
if p == nilNode {
var dft T
return dft, false
}
return p.Key(), true
}
================================================
FILE: structure/tree/tree_test.go
================================================
package tree_test
import (
"math/rand"
"reflect"
"sort"
"testing"
bt "github.com/TheAlgorithms/Go/structure/tree"
)
func TestTreeGetOrHas(t *testing.T) {
helper := func(tree TestTree[int], nums []int) {
tree.Push(nums...)
for _, num := range nums {
if !tree.Has(num) {
t.Errorf("Error with Has or Push method")
}
}
min, _ := tree.Min()
max, _ := tree.Max()
if _, ok := tree.Get(min - 1); ok {
t.Errorf("Error with Get method")
}
if _, ok := tree.Get(max + 1); ok {
t.Errorf("Error with Get method")
}
}
lens := []int{100, 1_000, 10_000, 100_000}
for _, ll := range lens {
nums := rand.Perm(ll)
t.Run("Test Binary Search Tree", func(t *testing.T) {
bsTree := bt.NewBinarySearch[int]()
helper(bsTree, nums)
})
t.Run("Test Red-Black Tree", func(t *testing.T) {
rbTree := bt.NewRB[int]()
helper(rbTree, nums)
})
t.Run("Test AVL Tree", func(t *testing.T) {
avlTree := bt.NewAVL[int]()
helper(avlTree, nums)
})
}
}
func TestTreePreOrder(t *testing.T) {
t.Run("Test for Binary-Search Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 70, 85, 100, 95, 105}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, []int{90, 80, 70, 1, 21, 31, 41, 51, 61, 71, 85, 100, 95, 105}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
}
for _, tt := range tests {
tree := bt.NewBinarySearch[int]()
tree.Push(tt.input...)
if ret := tree.PreOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("Error with PreOrder")
}
}
})
t.Run("Test for AVL Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 70, 85, 100, 95, 105}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, []int{70, 41, 21, 1, 31, 51, 61, 90, 80, 71, 85, 100, 95, 105}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{7, 3, 2, 1, 5, 4, 6, 9, 8, 10}},
}
for _, tt := range tests {
tree := bt.NewAVL[int]()
tree.Push(tt.input...)
if ret := tree.PreOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("Error with PreOrder")
}
}
})
t.Run("Test for Red-Black Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 70, 85, 100, 95, 105}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, []int{80, 41, 21, 1, 31, 61, 51, 70, 71, 90, 85, 100, 95, 105}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{7, 5, 3, 2, 1, 4, 6, 9, 8, 10}},
}
for _, tt := range tests {
tree := bt.NewRB[int]()
tree.Push(tt.input...)
if ret := tree.PreOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("Error with PreOrder")
}
}
})
}
func TestTreeInOrder(t *testing.T) {
lens := []int{100, 1_000, 10_000, 100_000}
for _, ll := range lens {
nums := rand.Perm(ll)
t.Run("Test Binary Search Tree", func(t *testing.T) {
bsTree := bt.NewBinarySearch[int]()
bsTree.Push(nums...)
if ret := bsTree.InOrder(); !sort.IntsAreSorted(ret) {
t.Errorf("Error with InOrder")
}
})
t.Run("Test Red-Black Tree", func(t *testing.T) {
rbTree := bt.NewRB[int]()
rbTree.Push(nums...)
if ret := rbTree.InOrder(); !sort.IntsAreSorted(ret) {
t.Errorf("Error with InOrder")
}
})
t.Run("Test AVL Tree", func(t *testing.T) {
avlTree := bt.NewAVL[int]()
avlTree.Push(nums...)
if ret := avlTree.InOrder(); !sort.IntsAreSorted(ret) {
t.Errorf("Error with InOrder")
}
})
}
}
func TestTreePostOrder(t *testing.T) {
t.Run("Test for Binary-Search Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{70, 85, 80, 95, 105, 100, 90}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[]int{61, 51, 41, 31, 21, 1, 71, 70, 85, 80, 95, 105, 100, 90}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
}
for i, tt := range tests {
tree := bt.NewBinarySearch[int]()
tree.Push(tt.input...)
if ret := tree.PostOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with Post", i)
}
}
})
t.Run("Test for AVL Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{70, 85, 80, 95, 105, 100, 90}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[]int{1, 31, 21, 61, 51, 41, 71, 85, 80, 95, 105, 100, 90, 70}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{1, 2, 4, 6, 5, 3, 8, 10, 9, 7}},
}
for i, tt := range tests {
tree := bt.NewAVL[int]()
tree.Push(tt.input...)
if ret := tree.PostOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with PostOrder", i)
}
}
})
t.Run("Test for Red-Black Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{70, 85, 80, 95, 105, 100, 90}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[]int{1, 31, 21, 51, 71, 70, 61, 41, 85, 95, 105, 100, 90, 80}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{1, 2, 4, 3, 6, 5, 8, 10, 9, 7}},
}
for i, tt := range tests {
tree := bt.NewRB[int]()
tree.Push(tt.input...)
if ret := tree.PostOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with PostOrder", i)
}
}
})
}
func TestTreeLevelOrder(t *testing.T) {
t.Run("Test for Binary-Search Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 100, 70, 85, 95, 105}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[]int{90, 80, 100, 70, 85, 95, 105, 1, 71, 21, 31, 41, 51, 61}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
}
for i, tt := range tests {
tree := bt.NewBinarySearch[int]()
tree.Push(tt.input...)
if ret := tree.LevelOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with LevelOrder", i)
}
}
})
t.Run("Test for AVL Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 100, 70, 85, 95, 105}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[]int{70, 41, 90, 21, 51, 80, 100, 1, 31, 61, 71, 85, 95, 105}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{7, 3, 9, 2, 5, 8, 10, 1, 4, 6}},
}
for i, tt := range tests {
tree := bt.NewAVL[int]()
tree.Push(tt.input...)
if ret := tree.LevelOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with LevelOrder", i)
}
}
})
t.Run("Test for Red-Black Tree", func(t *testing.T) {
tests := []struct {
input []int
want []int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 100, 70, 85, 95, 105}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[]int{80, 41, 90, 21, 61, 85, 100, 1, 31, 51, 70, 95, 105, 71}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{7, 5, 9, 3, 6, 8, 10, 2, 4, 1}},
}
for i, tt := range tests {
tree := bt.NewRB[int]()
tree.Push(tt.input...)
if ret := tree.LevelOrder(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with LevelOrder %v", i, ret)
}
}
})
}
func TestTreeMinAndMax(t *testing.T) {
helper := func(tree TestTree[int], nums []int) {
ll := len(nums)
if _, ok := tree.Min(); ok {
t.Errorf("Error with Min method.")
}
if _, ok := tree.Max(); ok {
t.Errorf("Error with Max method.")
}
tree.Push(nums...)
if min, ok := tree.Min(); !ok || min != nums[0] {
t.Errorf("Error with Min method.")
}
if max, ok := tree.Max(); !ok || max != nums[ll-1] {
t.Errorf("Error with Max method.")
}
}
lens := []int{500, 1_000, 10_000}
for _, ll := range lens {
nums := rand.Perm(ll)
sort.Ints(nums)
t.Run("Test Binary Search Tree", func(t *testing.T) {
helper(bt.NewBinarySearch[int](), nums)
})
t.Run("Test Red-Black Tree", func(t *testing.T) {
helper(bt.NewRB[int](), nums)
})
t.Run("Test AVL Tree", func(t *testing.T) {
helper(bt.NewAVL[int](), nums)
})
}
}
func TestTreeDepth(t *testing.T) {
t.Run("Test for Binary-Search Tree", func(t *testing.T) {
tests := []struct {
input []int
want int
}{
{[]int{}, 0},
{[]int{90, 80, 100, 70, 85, 95, 105}, 3},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, 9},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 10},
}
for _, tt := range tests {
tree := bt.NewBinarySearch[int]()
tree.Push(tt.input...)
if ret := tree.Depth(); ret != tt.want {
t.Errorf("Error with Depth")
}
}
})
t.Run("Test for AVL Tree", func(t *testing.T) {
tests := []struct {
input []int
want int
}{
{[]int{}, 0},
{[]int{90, 80, 100, 70, 85, 95, 105}, 3},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, 4},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 4},
}
for i, tt := range tests {
tree := bt.NewAVL[int]()
tree.Push(tt.input...)
if ret := tree.Depth(); ret != tt.want {
t.Errorf("#%d Error with Depth", i)
}
}
})
t.Run("Test for Red-Black Tree", func(t *testing.T) {
tests := []struct {
input []int
want int
}{
{[]int{}, 0},
{[]int{90, 80, 100, 70, 85, 95, 105}, 3},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, 5},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 5},
}
for i, tt := range tests {
tree := bt.NewRB[int]()
tree.Push(tt.input...)
if ret := tree.Depth(); ret != tt.want {
t.Errorf("#%d Error with Depth", i)
}
}
})
}
func TestTreeAccessNodesByLayer(t *testing.T) {
t.Run("Test for Binary-Search Tree", func(t *testing.T) {
tests := []struct {
input []int
want [][]int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, [][]int{{90}, {80, 100}, {70, 85, 95, 105}}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[][]int{{90}, {80, 100}, {70, 85, 95, 105}, {1, 71}, {21}, {31}, {41}, {51}, {61}}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, [][]int{{10}, {9}, {8}, {7}, {6}, {5}, {4}, {3}, {2}, {1}}},
{[]int{}, [][]int{}},
}
for i, tt := range tests {
tree := bt.NewBinarySearch[int]()
tree.Push(tt.input...)
if ret := tree.AccessNodesByLayer(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with AccessNoedsByLayer", i)
}
}
})
t.Run("Test for AVL Tree", func(t *testing.T) {
tests := []struct {
input []int
want [][]int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, [][]int{{90}, {80, 100}, {70, 85, 95, 105}}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[][]int{{70}, {41, 90}, {21, 51, 80, 100}, {1, 31, 61, 71, 85, 95, 105}}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, [][]int{{7}, {3, 9}, {2, 5, 8, 10}, {1, 4, 6}}},
{[]int{}, [][]int{}},
}
for i, tt := range tests {
tree := bt.NewAVL[int]()
tree.Push(tt.input...)
if ret := tree.AccessNodesByLayer(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with AccessNoedsByLayer", i)
}
}
})
t.Run("Test for Red-Black Tree", func(t *testing.T) {
tests := []struct {
input []int
want [][]int
}{
{[]int{90, 80, 100, 70, 85, 95, 105}, [][]int{{90}, {80, 100}, {70, 85, 95, 105}}},
{[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
[][]int{{80}, {41, 90}, {21, 61, 85, 100}, {1, 31, 51, 70, 95, 105}, {71}}},
{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, [][]int{{7}, {5, 9}, {3, 6, 8, 10}, {2, 4}, {1}}},
{[]int{}, [][]int{}},
}
for i, tt := range tests {
tree := bt.NewRB[int]()
t.Log(reflect.TypeOf(tree).String() == "*tree.RB[int]")
tree.Push(tt.input...)
if ret := tree.AccessNodesByLayer(); !reflect.DeepEqual(ret, tt.want) {
t.Errorf("#%d Error with AccessNoedsByLayer, %v", i, ret)
}
}
})
}
func TestTreePredecessorAndSuccessor(t *testing.T) {
helper := func(tree TestTree[int]) {
nums := []int{10, 8, 88, 888, 4, -1, 100}
tree.Push(nums...)
if ret, ok := tree.Predecessor(100); !ok || ret != 88 {
t.Error("Error with Predecessor")
}
if _, ok := tree.Predecessor(-1); ok {
t.Error("Error with Predecessor")
}
tree.Push(-100)
if ret, ok := tree.Predecessor(-1); !ok || ret != -100 {
t.Error("Error with Predecessor")
}
if _, ok := tree.Predecessor(-12); ok {
t.Error("Error with Predecessor")
}
if ret, ok := tree.Predecessor(4); !ok || ret != -1 {
t.Error("Error with Predecessor")
}
if ret, ok := tree.Successor(4); !ok || ret != 8 {
t.Error("Error with Successor")
}
if ret, ok := tree.Successor(8); !ok || ret != 10 {
t.Error("Error with Successor")
}
if ret, ok := tree.Successor(88); !ok || ret != 100 {
t.Error("Error with Successor")
}
if ret, ok := tree.Successor(100); !ok || ret != 888 {
t.Error("Error with Successor")
}
tree.Delete(888)
if _, ok := tree.Successor(100); ok {
t.Error("Error with Successor")
}
if ret, ok := tree.Successor(-1); !ok || ret != 4 {
t.Error("Error with Successor")
}
if _, ok := tree.Successor(888); ok {
t.Error("Error with Successor")
}
if _, ok := tree.Successor(188); ok {
t.Error("Error with Successor")
}
}
t.Run("Test for Binary Search Tree", func(t *testing.T) {
tree := bt.NewBinarySearch[int]()
helper(tree)
})
t.Run("Test for Red-Black Tree", func(t *testing.T) {
tree := bt.NewRB[int]()
helper(tree)
})
t.Run("Test for AVL Tree", func(t *testing.T) {
tree := bt.NewAVL[int]()
helper(tree)
})
}
// Benchmark the comparisons between BST, AVL and RB Tree
const testNum = 10_000
func BenchmarkBSTree_Insert(b *testing.B) {
helper := func() {
tree := bt.NewBinarySearch[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkBSTree_Has(b *testing.B) {
helper := func() {
tree := bt.NewBinarySearch[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
for i := 1; i <= testNum; i++ {
tree.Has(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkBSTree_Delete(b *testing.B) {
helper := func() {
tree := bt.NewBinarySearch[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
for i := 1; i <= testNum; i++ {
tree.Delete(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkRBTree_Insert(b *testing.B) {
helper := func() {
tree := bt.NewRB[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkRBTree_Has(b *testing.B) {
helper := func() {
tree := bt.NewRB[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
for i := 1; i <= testNum; i++ {
tree.Has(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkRBTree_Delete(b *testing.B) {
helper := func() {
tree := bt.NewRB[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
for i := 1; i <= testNum; i++ {
tree.Delete(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkAVLTree_Insert(b *testing.B) {
helper := func() {
tree := bt.NewAVL[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkAVLTree_Has(b *testing.B) {
helper := func() {
tree := bt.NewAVL[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
for i := 1; i <= testNum; i++ {
tree.Has(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
func BenchmarkAVLTree_Delete(b *testing.B) {
helper := func() {
tree := bt.NewAVL[int]()
for i := 1; i <= testNum; i++ {
tree.Push(i)
}
for i := 1; i <= testNum; i++ {
tree.Delete(i)
}
}
for i := 0; i < b.N; i++ {
helper()
}
}
================================================
FILE: structure/trie/trie.go
================================================
// Package trie provides Trie data structures in golang.
//
// Wikipedia: https://en.wikipedia.org/wiki/Trie
package trie
// Node represents each node in Trie.
type Node struct {
children map[rune]*Node // map children nodes
isLeaf bool // current node value
}
// NewNode creates a new Trie node with initialized
// children map.
func NewNode() *Node {
n := &Node{}
n.children = make(map[rune]*Node)
n.isLeaf = false
return n
}
// insert a single word at a Trie node.
func (n *Node) insert(s string) {
curr := n
for _, c := range s {
next, ok := curr.children[c]
if !ok {
next = NewNode()
curr.children[c] = next
}
curr = next
}
curr.isLeaf = true
}
// Insert zero, one or more words at a Trie node.
func (n *Node) Insert(s ...string) {
for _, ss := range s {
n.insert(ss)
}
}
// Find words at a Trie node.
func (n *Node) Find(s string) bool {
next, ok := n, false
for _, c := range s {
next, ok = next.children[c]
if !ok {
return false
}
}
return next.isLeaf
}
// Capacity returns the number of nodes in the Trie
func (n *Node) Capacity() int {
r := 0
for _, c := range n.children {
r += c.Capacity()
}
return 1 + r
}
// Size returns the number of words in the Trie
func (n *Node) Size() int {
r := 0
for _, c := range n.children {
r += c.Size()
}
if n.isLeaf {
r++
}
return r
}
// remove lazily a word from the Trie node, no node is actually removed.
func (n *Node) remove(s string) {
if len(s) == 0 {
return
}
next, ok := n, false
for _, c := range s {
next, ok = next.children[c]
if !ok {
// word cannot be found - we're done !
return
}
}
next.isLeaf = false
}
// Remove zero, one or more words lazily from the Trie, no node is actually removed.
func (n *Node) Remove(s ...string) {
for _, ss := range s {
n.remove(ss)
}
}
// Compact will remove unecessay nodes, reducing the capacity, returning true if node n itself should be removed.
func (n *Node) Compact() (remove bool) {
for r, c := range n.children {
if c.Compact() {
delete(n.children, r)
}
}
return !n.isLeaf && len(n.children) == 0
}
================================================
FILE: structure/trie/trie_bench_test.go
================================================
package trie
import (
"fmt"
"math/rand"
"testing"
)
// CAUTION : make sure to limit the benchmarks to 3000 iterations,
// or removal will mostly process an empty Trie, giving absurd results.
func BenchmarkTrie_Insert(b *testing.B) {
insert := make([]string, 3000)
for i := 0; i < len(insert); i++ {
insert[i] = fmt.Sprintf("%f", rand.Float64())
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
n := NewNode()
n.Insert(insert...)
}
}
func BenchmarkTrie_Find_non_existent(b *testing.B) {
insert := make([]string, 3000)
for i := 0; i < len(insert); i++ {
insert[i] = fmt.Sprintf("%f", rand.Float64())
}
n := NewNode()
n.Insert(insert...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.Find("0.3213213244346546546546565465465") // does not exists
}
}
func BenchmarkTrie_Find_existent(b *testing.B) {
insert := make([]string, 3000)
for i := 0; i < len(insert); i++ {
insert[i] = fmt.Sprintf("%f", rand.Float64())
}
n := NewNode()
n.Insert(insert...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.Find(insert[i%3000]) // always exists !
}
}
func BenchmarkTrie_Remove_lazy(b *testing.B) {
insert := make([]string, 3000)
for i := 0; i < len(insert); i++ {
insert[i] = fmt.Sprintf("%f", rand.Float64())
}
n := NewNode()
n.Insert(insert...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.Remove(insert[i%3000]) // exists, at least until removed ...
}
}
func BenchmarkTrie_Remove_and_Compact(b *testing.B) {
insert := make([]string, 3000)
for i := 0; i < len(insert); i++ {
insert[i] = fmt.Sprintf("%f", rand.Float64())
}
n := NewNode()
n.Insert(insert...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.Remove(insert[i%3000])
n.Compact()
}
}
================================================
FILE: structure/trie/trie_test.go
================================================
package trie
import (
"testing"
)
func TestTrieInsert(t *testing.T) {
n := NewNode()
insertWords := []string{
"nikola",
"tesla",
}
checkWords := map[string]bool{
"thomas": false,
"edison": false,
"nikola": true,
}
n.Insert(insertWords...)
n.verify(t, checkWords)
n.verifySizeCapa(t, 2, 12)
}
func TestTrieInsert_substrings(t *testing.T) {
n := NewNode()
insertWords := []string{
"aa",
"aaaa",
"aaaaa",
}
checkWords := map[string]bool{
"a": false,
"aa": true,
"aaa": false,
"aaaa": true,
"aaaaa": true,
"aaaaaa": false,
"aaaaaaa": false,
}
n.Insert(insertWords...)
n.verify(t, checkWords)
n.verifySizeCapa(t, 3, 5+1)
n.Remove("aaaa")
checkWords["aaaa"] = false
n.verify(t, checkWords)
n.verifySizeCapa(t, 2, 5+1)
if n.Compact() {
t.Fatalf("it should not be possible to remove the node")
}
n.verify(t, checkWords)
n.verifySizeCapa(t, 2, 5+1)
}
func TestTrieRemove(t *testing.T) {
n := NewNode()
insertWords := []string{
"nikola",
"tesla",
"albert",
"einstein",
}
checkWords := map[string]bool{
"thomas": false,
"edison": false,
"nikola": true,
"albert": true,
"einstein": true,
}
n.Insert(insertWords...)
n.verify(t, checkWords)
size, capa := n.Size(), n.Capacity() // 4 words ...
n.Remove("albert")
checkWords["albert"] = false
size-- // 3 words in size, but no change in capacity
n.verify(t, checkWords)
n.verifySizeCapa(t, size, capa)
n.Remove("albert") // no effect since already removed
n.verify(t, checkWords)
n.verifySizeCapa(t, size, capa)
n.Remove("marcel") // no effect since ,o, existent
n.verify(t, checkWords)
n.verifySizeCapa(t, size, capa)
n.Remove("nikola", "tesla") // 1 word
checkWords["nikola"] = false
checkWords["tesla"] = false
size -= 2 // t1 word left, but still no change in capacity
n.verify(t, checkWords)
n.verifySizeCapa(t, size, capa)
// compact the Tree
if n.Compact() {
t.Fatal("the Trie should not be completely removable after compaction")
}
if capa <= n.Capacity() {
t.Fatal("capacity should have reduced following compaction")
}
capa = n.Capacity()
n.verify(t, checkWords)
n.verifySizeCapa(t, size, capa) // still 1 word, reduced capacity
n.Remove("einstein")
checkWords["einstein"] = false
size-- // No more words
n.verify(t, checkWords)
n.verifySizeCapa(t, size, capa) // no words, but still have some nodes left capacity
if !n.Compact() {
t.Fatal("the root node of an empty Trie should be marked as removable after compaction")
}
n.verifySizeCapa(t, 0, 1) // no words, only the root node left
}
// --------------- helper functions ---------------------------
// verify if provided words are present
func (n *Node) verify(t *testing.T, checkWords map[string]bool) {
for k, v := range checkWords {
ok := n.Find(k)
if ok != v {
t.Fatalf(
"%q is %s supposed to be in the Trie.",
k,
map[bool]string{true: "", false: "NOT "}[v],
)
}
// t.Logf(
// "\"%s\" is %sin the Trie.",
// k,
// map[bool]string{true: "", false: "NOT "}[ok],
// )
}
}
// verify expected size and capacity
func (n *Node) verifySizeCapa(t *testing.T, expectedSize, expectedCapacity int) {
if got := n.Size(); got != expectedSize {
t.Fatalf("Expected Size was %d but got %d", expectedSize, got)
}
if got := n.Capacity(); got != expectedCapacity {
t.Fatalf("Expected Capacity was %d but got %d", expectedCapacity, got)
}
}
================================================
FILE: structure/trie/trieexample_test.go
================================================
package trie
import "fmt"
func ExampleNode() {
// creates a new node
node := NewNode()
// adds words
node.Insert("nikola")
node.Insert("tesla")
// check size and capacity
fmt.Println(node.Size()) // 2 words
fmt.Println(node.Capacity()) // 12 nodes
// finds words
fmt.Println(node.Find("thomas")) // false
fmt.Println(node.Find("edison")) // false
fmt.Println(node.Find("nikola")) // true
// remove a word, and check it is gone
node.Remove("tesla")
fmt.Println(node.Find("tesla")) // false
// size and capacity have changed
fmt.Println(node.Size()) // 1 word left
fmt.Println(node.Capacity()) // 12 nodes remaining
// output:
// 2
// 12
// false
// false
// true
// false
// 1
// 12
}