
Rapidkode:第一次正確地
Rapidkode是一個Python軟件包,可提供快速,靈活和表現力的數據結構,旨在使競爭性編程和編碼變得容易且直觀的鋁製。它的目標是成為Python中競爭性編程的基本高級構建塊。在Rapidkode的情況下,您可以在更短的時間內執行複雜的算法,所有算法都可以優化到最佳的時間,以減少時間複雜性,以使您在領導者董事會中脫穎而出。不再有時間浪費時間寫大量的代碼,然後再調試它們,而Rapidkode只用一行一行代碼就可以在您的指尖上發生。 Rapidkode的目的是幫助初學者開始競爭性編程,了解時空的重要性。製作RapidKode的座右銘是“第一次正確地將其正確”,而不是花10分鐘的時間在UTIL功能上。
安裝:
或者
有關問題,錯誤報告和貢獻訪問開發存儲庫 - >單擊此處
可用功能:
數字功能:
| 句法 | 手術 |
|---|
| numbers.gen_sparsenum_upto(x) | 生成稀疏數字到給定範圍 |
| numbers.get_sparsenum_after(n) | 返回給定號碼的稀疏數字 |
| numbers.checkprime(x) | 如果數字為PRIME,則返回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 fibonacci編號 |
例子:
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')。到('new_sys') | 將X從系統轉換為新系統 |
| 例子: | |
| 轉換(9845,'dec')。到('bin') | 將9845從十進制轉換為二進制 |
| 轉換(3745,'oct')。到('hex') | 將3745從八分之一轉換為十六進制 |
- 您可以用['bin','dec','oct','hex']和
new_sys用['bin','','dec','oct','hex']替換為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) | 返回x在ARR中的位置 | 在) |
| 二進制搜索 | binary.search(arr,x) | 返回x在ARR中的位置 | o(log n) |
| 跳躍搜索 | jump.search(arr,x) | 返回x在ARR中的位置 | o(√n) |
| 插值搜索 | interpolation.search(arr,x) | 返回x在ARR中的位置 | o(log2(log2 n)) |
| 指數搜索 | endusential.search(arr,x) | 返回x在ARR中的位置 | o(log2 i) |
| 三元搜索 | ternary.search(arr,x) | 返回x在ARR中的位置 | o(log3 n) |
添加劑您可以使用:
| 功能 | 手術 |
|---|
| 。展示() | 在終端中打印代碼 |
| 。資訊() | 提供簡短的信息 |
| .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.
三個函數.show() ,. .info() ,. .algo()可用於所有6種搜索技術。
排序算法:
| 技術 | 句法 | 手術 | 時間複雜性 |
|---|
| 選擇排序 | Selection.Sort(ARR) | 分類並返回給定的數組 | o(n^2) |
| 氣泡排序 | bubble.sort(arr) | 分類並返回給定的數組 | o(n^2) |
| 插入排序 | 插入(arr) | 分類並返回給定的數組 | o(n^2) |
| 合併排序 | Merge.Sort(ARR) | 分類並返回給定的數組 | o(n log(n)) |
| 堆排序 | HEAP.SORT(ARR) | 分類並返回給定的數組 | o(n log(n)) |
| 快速排序 | quick.sort(開始,結束,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) |
| pigeongole排序 | PIGEONHOLE.SORT(ARR) | 分類並返回給定的數組 | o(n + n) |
| 週期排序 | cycle.sort(arr) | 分類並返回給定的數組 | o(n2) |
添加劑您可以使用:
| 功能 | 手術 |
|---|
| 。展示() | 在終端中打印代碼 |
| 。資訊() | 提供簡短的信息 |
| .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.
三個函數.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算法,該算法是使用哈希函數在文本中用於搜索/匹配模式的算法。與匹配算法的幼稚字符串不同,它不會在初始階段中傳播每個字符,而是過濾不匹配的字符,然後執行比較。
| 句法 | 手術 |
|---|
| tatter.isthere(a).inn(b) | 如果字符串B中存在字符串A,則返回true |
| 模式。在任何地方(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 ))
輸出:
linkedlist函數:
| 手術 | 句法 |
|---|
| .ins_beg(節點) | 在開始時插入一個新節點 |
| .ins_end(節點) | 插入一個新節點 |
| .ins_after(POS,節點) | 在指定節點之後插入一個新節點 |
| .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) | 返回DEC編號中的套件編號 |
| bits.Rotate_Byleft(x,d) | 旋轉到左D次的位 |
| bits.Rotate_Byright(x,d) | 旋轉到左D次的位 |
| bits.countflips(x,y) | 返回flips的數字,將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
其他雜項功能:
| 句法 | 手術 |
|---|
| .showsieves() | 打印sieves代碼以查找終端中的質子數 |
| getPrimeFactors.fornum(x) | 返回給定數字的主要因素列表 |
| Findgcdof(x,y) | 返回給定數字的GCD |
| findinversions.forr(arr) | 返回陣列的分類程度 |
| catlan_numbers.getElement(x) | 返回X'th Catlan號碼 |
| catlan_numbers.gen(x) | 返回第一個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❤️
快樂的快速koding!