Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit40b9fc4

Browse files
authored
Merge branch 'master' into add-heap-sort
2 parentsf60068d +4cb0741 commit40b9fc4

File tree

10 files changed

+174
-4
lines changed

10 files changed

+174
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Shell Sort Algorithm
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: Elias
7+
# Github: @eliasbayona
8+
#
9+
10+
defshell_sort(arr):
11+
n=len(arr)
12+
h=int(n/2)
13+
14+
15+
whileh>0:
16+
foriinrange(h,n):
17+
18+
temp=arr[i]
19+
j=i
20+
21+
whilej>=handarr[j-h]>temp:
22+
arr[j]=arr[j-h]
23+
j-=h
24+
25+
arr[j]=temp
26+
27+
h=int(h/2)
28+
29+
returnarr
30+

‎allalgorithms/string/__init__.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .palindrome_checkimport*
2-
2+
from .is_uniqueimport*
3+
from .hamming_distimport*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Binary search works for a sorted array.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: ninexball
7+
# Github: @ninexball
8+
#
9+
10+
defhamming_dist(seq1:str,seq2:str)->int:
11+
"""Compare hamming distance of two strings"""
12+
iflen(seq1)!=len(seq2):
13+
raiseValueError("length of strings are not the same")
14+
returnsum(c1!=c2forc1,c2inzip(seq1,seq2))

‎allalgorithms/string/is_unique.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Check if a string has all unique characters.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: José E. Andrade Jr.
7+
# Github: @andradejunior
8+
#
9+
10+
defis_unique(string_to_check):
11+
character_set=set()
12+
forcharacterinstring_to_check:
13+
ifcharacterincharacter_set:
14+
returnFalse
15+
character_set.add(character)
16+
returnTrue

‎docs/sorting/shell-sort.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#Selection Sort
2+
3+
In computer science, shell sort improves upon insertion sort by moving out of order elements more than one position at a time. It has a best case O(n log n) time complexity, and for other cases, it depends on the gap sequence. According to Poonen Theorem, worst case complexity for shell sort is Θ(NlogN)^2/(log logN)^2) or Θ(NlogN)^2/log logN) or Θ(N(logN)^2) or something in between.
4+
##Install
5+
6+
```
7+
pip install allalgorithms
8+
```
9+
10+
##Usage
11+
12+
```py
13+
from allalgorithms.sortingimport shell_sort
14+
15+
arr= [77,2,10,-2,1,7]
16+
17+
print(shell_sort(arr))
18+
# -> [-2, 1, 2, 7, 10, 77]
19+
```
20+
21+
##API
22+
23+
###selection_sort(array)
24+
25+
>Returns a sorted array
26+
27+
#####Params:
28+
29+
-`array`: Unsorted Array

‎docs/string/hamming-dist.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#Hamming Distance
2+
3+
In informatics, Hamming distance is the number of positions where the characters differ between two strings of equal length
4+
5+
##Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
##Usage
12+
13+
```
14+
>>> from allalgorithms.sorting import hamming_dist
15+
16+
>>> hamming_dist("hello world", "hello wario")
17+
3
18+
```
19+
20+
##API
21+
22+
###hamming_dist(seq1, seq2)
23+
24+
>Returns an integer
25+
26+
>Raises a ValueError if strings are of unequal length
27+
28+
#####Params:
29+
30+
-`seq1`: first string to compare
31+
-`seq2`: second string to compare
32+

‎docs/string/is-unique.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Is Unique
2+
3+
This algorithms checks if a string has all unique characters in O(n) time.
4+
5+
##Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
##Usage
12+
13+
```py
14+
from allalgorithms.stringimport is_unique
15+
16+
str="abcdefg"
17+
18+
print(is_unique(str)
19+
# -> True
20+
21+
str="test"
22+
23+
print(is_unique(str)
24+
# -> False
25+
```
26+
27+
## API
28+
29+
### is_unique(string)
30+
31+
> ReturnTrueif string hasall unique characters,False otherwise
32+
33+
##### Params:
34+
35+
-`string`: Input String
36+

‎readme.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ print(binary_search(arr, 3))
6161
-[Binary Search](https://python.allalgorithms.com/searches/binary-search)
6262
-[Fibonacci Search](https://python.allalgorithms.com/searches/fibonacci-search)
6363
-[Jump Search](https://python.allalgorithms.com/searches/jump-search)
64-
64+
6565
-###Sorting
6666
-[Bubble Sort](https://python.allalgorithms.com/sorting/bubble-sort)
6767
-[Cocktail Shaker Sort](https://python.allalgorithms.com/sorting/cocktail-shaker-sort)
@@ -75,7 +75,7 @@ print(binary_search(arr, 3))
7575

7676
#Related
7777

78-
-[allalgorithms-javascript](https://github.com/abranhe/allalgorithms-javascript): All ▲lgorithms Javascript library
78+
-[allalgorithms-js](https://github.com/abranhe/allalgorithms-js): All ▲lgorithms Javascript library
7979

8080
#Maintainers
8181

‎tests/test_sorting.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
cocktail_shaker_sort,
1111
tree_sort,
1212
heap_sort
13+
shell_sort,
1314
)
1415

1516

@@ -43,6 +44,8 @@ def test_heap_sort(self):
4344
heap_sort(array)
4445
self.assertEqual([-44,1,2,3,7,19],array)
4546

47+
deftest_shell_sort(self):
48+
self.assertEqual([-44,1,2,3,7,19],shell_sort([7,3,2,19,-44,1]))
4649

4750
if__name__=="__main__":
4851
unittest.main()

‎tests/test_string.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
importunittest
22

3-
fromallalgorithms.stringimportpalindrome_check
3+
fromallalgorithms.stringimportpalindrome_check,is_unique
44

55

66
classTestSorting(unittest.TestCase):
@@ -13,5 +13,14 @@ def test_palindrome_check(self):
1313
self.assertEqual(True,palindrome_check("Was it a car or a cat I saw?"))
1414
self.assertEqual(False,palindrome_check("How are you?"))
1515

16+
deftest_is_unique(self):
17+
self.assertEqual(True,is_unique("abcdefg"))
18+
self.assertEqual(True,is_unique("1234567"))
19+
self.assertEqual(True,is_unique("algorithms"))
20+
self.assertEqual(False,is_unique("abcdefa"))
21+
self.assertEqual(False,is_unique("abddefg"))
22+
self.assertEqual(False,is_unique("12345567"))
23+
24+
1625
if__name__=="__main__":
1726
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp