
RapidKode:初めて正しくしてください
RapidKodeは、競争力のあるプログラミングとコーディングの両方を簡単かつ直感的にするために設計された高速で柔軟で表現力のあるデータ構造を提供するPythonパッケージです。 Pythonでの競争力のあるプログラミングの基本的な高レベルのビルドブロックになることを目的としています。rapidKodeで複雑なアルゴリズムを実行できます。すべてのアルゴリズムは、リーダーボードで目立つために時間の複雑さを減らすために最高に最適化されています。 RapidKodeを使用して、コードの巨大なチャックを書いて後でデバッグするのに時間はありません。 RapidKodeの目的は、初心者が競争力のあるプログラミングを開始し、時間と空間の重要性を理解するのを支援することです。 RapidKodeを作成するモットーは、UTIL関数に10分の貴重な数分を費やすのではなく、「初めて正しくする」ことです。
インストール:
または
問題については、バグレポートと貢献の開発リポジトリをご覧ください - >ここをクリックしてください
利用可能な機能:
数字関数:
| 構文 | 手術 |
|---|
| numbers.gen_sparsenum_upto(x) | 指定された範囲までスパース数を生成します |
| numbers.get_sparsenum_after(n) | 指定された数の成功したスパース数を返します |
| numbers.checkprime(x) | 数字がプライムの場合はtrueを返します |
| numbers.getPrimes.generate(x) | 最初のx素数を返します |
| numbers.getPrimes.upto(x) | 指定された範囲にプライムナンバーを返します |
| numbers.getPrimes.inrange(x、y) | 指定された範囲の素数を返します |
| numbers.fib.getElement(x) | x'th fibonacci番号を返します |
| numbers.fib.generate(x) | 最初のxフィボナッチ数を返します |
例:
import rapidkode as rk
var = rk . numbers . gen_sparsenum_upto ( 100 )
print ( var )
var = rk . numbers . get_sparsenum_after ( 3289 )
print ( var )
var = rk . numbers . checkprime ( 8364 )
print ( var )
var = rk . numbers . getprimes . generate ( 100 )
print ( var )
var = rk . numbers . getprimes . inrange ( 100 , 500 )
print ( var )
var = rk . numbers . fib . getelement ( 58 )
print ( var )
var = rk . numbers . fib . generate ( 25 )
print ( var )
出力:
[0, 1, 2, 4, 5, 8, 9, 10, 16, 17, 18, 20, 21, 32, 33, 34, 36, 37, 40, 41, 42, 64, 65, 66, 68, 69, 72, 73, 74, 80, 81, 82, 84, 85, 128]
4096
False
[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, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
365435296162
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368]
番号システム - 変換関数:
| 構文 | 手術 |
|---|
| 変換(x、 'sys')。to( 'new_sys') | XをSYSから新しいSYSに変換します |
| 例: | |
| 変換(9845、 'dec')。まで( 'bin') | 9845を10進数からバイナリに変換します |
| 変換(3745、 'Oct')。まで( 'hex') | 3745をOctalから16進状に変換します |
- ['bin'、 'dec'、 'oct'、 'hex']、および['bin'、 'dec'、 'oct'、 'hex']で['
sys '、 'dec'、 'oct'、 'hex']とnew_sys置き換えることができます。
import rapidkode as rk
converted_num_1 = rk . convert ( 2013 , 'dec' ). to ( 'bin' )
print ( converted_num_1 )
converted_num_2 = rk . convert ( 11111011101 , 'bin' ). to ( 'hex' )
print ( converted_num_2 )
converted_num_3 = rk . convert ( '7dd' , 'hex' ). to ( 'dec' )
print ( converted_num_3 )
converted_num_4 = rk . convert ( 5634 , 'oct' ). to ( 'dec' )
print ( converted_num_4 )
converted_num_5 = rk . convert ( 2972 , 'hex' ). to ( 'oct' )
print ( converted_num_5 )
converted_num_6 = rk . convert ( 24562 , 'oct' ). to ( 'bin' )
print ( converted_num_6 )
出力:
11111011101
7dd
2013
2972
24562
10100101110010
アルゴリズムの検索:
| 技術 | 構文 | 手術 | 時間の複雑さ |
|---|
| 線形検索 | linear.search(arr、x) | Arrのxの位置を返します | の上) |
| バイナリ検索 | binary.search(arr、x) | Arrのxの位置を返します | o(log n) |
| ジャンプ検索 | Jump.Search(arr、x) | Arrのxの位置を返します | o(√n) |
| 補間検索 | interpolation.search(arr、x) | Arrのxの位置を返します | o(log2(log2 n)) |
| 指数検索 | Exponential.search(arr、x) | Arrのxの位置を返します | o(log2 i) |
| 三元検索 | ternary.search(arr、x) | Arrのxの位置を返します | o(log3 n) |
additonallyあなたは使用できます:
| 関数 | 手術 |
|---|
| 。見せる() | 端末にコードを印刷します |
| 。情報() | 簡単な情報を提供します |
| .algo() | ステップワイズアルゴリズムを印刷します |
例:
import rapidkode as rk
> >> rk . binary . show ()
> >> rk . binary . info ()
> >> rk . binary . algo ()
出力:
def binarysearch ( arr , x ):
l = 0
r = len ( arr ) - 1
while l <= r :
mid = l + ( r - l ) // 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] < x :
l = mid + 1
else :
r = mid - 1
return "element not found"
Binary search is the search technique that works efficiently on sorted lists
. Hence, to search an element into some list using the binary search technique, we must ensure that the list is sorted
. Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is compared with the middle element of the list
. If the match is found then, the location of the middle element is returned
. Otherwise, we search into either of the halves depending upon the result produced through the match
Algorithm
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
Step 5 - If both are not matched, then check whether the search element is smaller or larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or until sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then display "Element is not found in the list!!!" and terminate the function.
3つの関数.show() 、. .info() 、. .algo() 6つの検索手法すべてに使用できます。
ソートアルゴリズム:
| 技術 | 構文 | 手術 | 時間の複雑さ |
|---|
| 選択ソート | selection.sort(arr) | 特定の配列をソートして返します | o(n^2) |
| バブルソート | bubble.sort(arr) | 特定の配列をソートして返します | o(n^2) |
| 挿入ソート | Insertion.sort(arr) | 特定の配列をソートして返します | o(n^2) |
| ソートをマージします | merge.sort(arr) | 特定の配列をソートして返します | o(n log(n)) |
| ヒープソート | heap.sort(arr) | 特定の配列をソートして返します | o(n log(n)) |
| クイックソート | Quick.sort(start、end、arr) | 特定の配列をソートして返します | o(n^2) |
| ソートをカウントします | count.sort(arr) | 特定の配列をソートして返します | o(n+k) |
| RADIXソート | radix.sort(arr) | 特定の配列をソートして返します | o(nk) |
| バケットソート | bucket.sort(arr) | 特定の配列をソートして返します | o(n + k) |
| シェルソート | shell.sort(arr) | 特定の配列をソートして返します | o(nlog n) |
| 櫛のソート | comb.sort(arr) | 特定の配列をソートして返します | o(n log n) |
| ピガゴールのソート | Pigeonhole.sort(arr) | 特定の配列をソートして返します | o(n + n) |
| サイクルソート | Cycle.Sort(arr) | 特定の配列をソートして返します | o(n2) |
additonallyあなたは使用できます:
| 関数 | 手術 |
|---|
| 。見せる() | 端末にコードを印刷します |
| 。情報() | 簡単な情報を提供します |
| .algo() | ステップワイズアルゴリズムを印刷します |
例:
import rapidkode as rk
> >> rk . count . show ()
> >> rk . count . info ()
> >> rk . count . algo ()
出力:
def countsort ( arr ):
output = [ 0 for i in range ( len ( arr ))]
count = [ 0 for i in range ( 256 )]
array = [ 0 for _ in arr ]
for i in arr :
count [ i ] += 1
for i in range ( 256 ):
count [ i ] += count [ i - 1 ]
for i in range ( len ( arr )):
output [ count [ arr [ i ]] - 1 ] = arr [ i ]
count [ arr [ i ]] -= 1
for i in range ( len ( arr )):
array [ i ] = output [ i ]
return array Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array
. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array
. Counting sort is a sorting technique based on keys between a specific range
. It works by counting the number of objects having distinct key values (kind of hashing)
. Then doing some arithmetic to calculate the position of each object in the output sequence
Algorithm
step 1 - Find out the maximum element (let it be max) from the given array.
step 2 - Initialize an array of length max+1 with all elements 0.
This array is used for storing the count of the elements in the array.
step 3 - Store the count of each element at their respective index in count array
For example: if the count of element 3 is 2 then, 2 is stored in the 3rd position of count array.
If element "5" is not present in the array, then 0 is stored in 5th position.
step 4 - Store cumulative sum of the elements of the count array.
It helps in placing the elements into the correct index of the sorted array.
step 5 - Find the index of each element of the original array in the count array.
This gives the cumulative count. Place the element at the index calculated as shown in figure below.
step 6 - After placing each element at its correct position, decrease its count by one.
3つの関数.show() 、. .info() 、. .algo() 、13のソートテクニックすべてに使用できます。
グラフ関数:
| 構文 | 手術 |
|---|
| .buildedge(u、v) | グラフエッジを作成します |
| .buildmultiedge([]) | 指定された座標リストを持つグラフを作成します |
| .bfs(x) | 幅の最初の検索を実行します |
| .dfs(x) | 深さの最初の検索を実行します |
| .findap() | グラフのアーティキュレーションポイントを返します |
例:
import rapidkode as rk
# make graph object with graph() class
my_graph = rk . graph ()
# adding one edge at a time
my_graph . buildedge ( 0 , 1 )
my_graph . buildedge ( 0 , 2 )
my_graph . buildedge ( 1 , 2 )
my_graph . buildedge ( 2 , 0 )
my_graph . buildedge ( 2 , 3 )
my_graph . buildedge ( 3 , 3 )
import rapidkode as rk
# make graph object with graph() class
my_graph = rk . graph ()
# adding multiple edges at once
my_graph . buildmultiedge ([ 0 , 1 , 0 , 2 , 1 , 2 , 2 , 0 , 2 , 3 , 3 , 3 ])
import rapidkode as rk
# make graph object with graph() class
my_graph = rk . graph ()
my_graph . buildmultiedge ([ 0 , 1 , 0 , 2 , 1 , 2 , 2 , 0 , 2 , 3 , 3 , 3 ])
# performing BFS from edge 2
print ( my_graph . BFS ( 2 ))
# performing DFS from edge 2
print ( my_graph . DFS ( 2 ))
# finding the Articulation Point
print ( my_graph . findAP ())
出力:
['-->', 2, '-->', 0, '-->', 3, '-->', 1]
['-->', 2, '-->', 0, '-->', 1, '-->', 3]
2
パターン関数:
次の関数は、ハッシュ関数を使用してテキストのパターンの検索/マッチングパターンに使用されるアルゴリズムであるRabin-Karpアルゴリズムを使用します。 Naive String Matchingアルゴリズムとは異なり、初期フェーズのすべての文字を通過するのではなく、一致しない文字をフィルタリングして比較を実行します。
| 構文 | 手術 |
|---|
| pattern.isthere(a).inn(b) | 文字列Aが文字列bに存在する場合、trueを返します |
| pattern.whereis(a).inn(b) | 文字列bで文字列Aのインデックス位置を返します |
例:
import rapidkode as rk
a = 'sasi'
b = 'satyasasivatsal'
print ( rk . isthere ( a ). inn ( b ))
print ( rk . whereis ( a ). inn ( b ))
出力:
LinkedListFunctions:
| 手術 | 構文 |
|---|
| .ins_beg(ノード) | 開始時に新しいノードを挿入します |
| .ins_end(ノード) | 最後に新しいノードを挿入します |
| .ins_after(pos、node) | ノードが指定された後に新しいノードを挿入します |
| .ins_before(pos、node) | 指定されたノードの前に新しいノードを挿入します |
| .del_node(ノード) | 指定されたノードを削除します |
| .return_as_list() | LinkedListをPythonリストとして返します |
例:
import rapidkode as rk
my_list = rk . linkedlist ()
my_list . head = rk . node ( 'a' )
s1 = rk . node ( 'b' )
s2 = rk . node ( 'c' )
s3 = rk . node ( 'd' )
s4 = rk . node ( 'e' )
s5 = rk . node ( 'f' )
s6 = rk . node ( 'g' )
my_list . head . next = s1
s1 . next = s2
s2 . next = s3
s3 . next = s4
s4 . next = s5
s5 . next = s6
print ( my_list )
出力:
a -> b -> c -> d -> e -> f -> g -> None
例-2:
# insertion at beginning
my_list . ins_beg ( rk . node ( 'A' ))
# insertion at end
my_list . ins_end ( rk . node ( 'G' ))
# insertion at positiom
my_list . ins_after ( 'e' , rk . node ( 'E' ))
# insertion at position
my_list . ins_before ( 'c' , rk . node ( 'C' ))
# deletion of ndoe
my_list . del_node ( 'b' )
# returning as list
my_listt = my_list . return_as_list ()
print ( my_list )
print ( my_listt )
出力:
A -> a -> C -> c -> d -> e -> E -> f -> g -> G -> None
['A', 'a', 'C', 'c', 'd', 'e', 'E', 'f', 'g', 'G', 'None']
ビット操作ファクション:
| 構文 | 手術 |
|---|
| bits.toggle_bits(x) | セットビットと非セットビットを切り替えます |
| bits.convert_to_bin(x) | 特定の番号をバイナリに変換します |
| bits.counsetbits(x) | No.ofセットビットを12月に返します |
| bits.rotate_byleft(x、d) | ビットを左に左に回転させます |
| bits.rotate_byright(x、d) | ビットを左に左に回転させます |
| bits.countflips(x、y) | No. ofフリップを返してxをyとして作成します |
例:
import rapidkode as rk
var = rk . bits . toggle_bits ( 873652 )
print ( var )
var = rk . bits . convert_to_bin ( 873652 )
print ( var )
var = rk . bits . countsetbits ( 873652 )
print ( var )
var = rk . bits . rotate_byleft ( 873652 , 4 )
print ( var )
var = rk . bits . rotate_byright ( 873652 , 4 )
print ( var )
var = rk . bits . countflips ( 8934756 , 873652 )
print ( var )
出力:
960632
11010101010010110100
8474306
13978432
54603
7
その他のMISC機能:
| 構文 | 手術 |
|---|
| .showsieves() | ターミナルで素数を見つけるためのシーブコードを印刷します |
| getPrimeFactors.fornum(x) | 指定された数の主要な要因のリストを返します |
| findgcdof(x、y) | 指定された数値のGCDを返します |
| FindInversions.forr(arr) | 配列がソートされることからどれだけ近いかを返します |
| catlan_numbers.getElement(x) | x'th catlan番号を返します |
| catlan_numbers.gen(x) | First x catlan_numbersのリストを返します |
例:
import rapidkode as rk
var = rk . getprimefactors . fornum ( 6754 )
print ( var )
var = rk . findgcdof ( 97345435 , 8764897 )
print ( var )
var = rk . findinversions . forr ([ 1 , 20 , 6 , 4 , 5 ])
print ( var )
var = rk . catlan_numbers . getelement ( 15 )
print ( var )
var = rk . catlan_numbers . gen ( 28 )
print ( var )
出力:
[2, 11, 307.0]
1
5
9694845.0
[1.0, 1.0, 2.0, 5.0, 14.0, 42.0, 132.0, 429.0, 1430.0, 4862.0, 16796.0, 58786.0, 208012.0, 742900.0, 2674440.0, 9694845.0, 35357670.0, 129644790.0, 477638700.0, 1767263190.0, 6564120420.0, 24466267020.0, 91482563640.0, 343059613650.0, 1289904147324.0, 4861946401452.0, 18367353072152.0, 69533550916004.0]
RapidKodeに貢献します
- すべての貢献、バグレポート、バグ修正、ドキュメントの改善、機能強化、アイデアは大歓迎です。
- 問題が見つかった場合は問題を提起します
- 貢献したい場合は、プルリクエストを行う前にリポジトリをフォークする前に問題を提起します。
- ロゴとヘッダーのクレジット - > M.SriHarsha❤️
ハッピーラピッドコーディング!!