
RapidKode: ทำให้ถูกต้องในครั้งแรก
RapidKode เป็นแพ็คเกจ Python ที่ให้โครงสร้างข้อมูลที่รวดเร็วยืดหยุ่นและแสดงออกได้ Alogorithms ที่ออกแบบมาเพื่อให้การทำงานกับการเขียนโปรแกรมและการเข้ารหัสง่ายและใช้งานง่าย มันมีจุดมุ่งหมายที่จะเป็นหน่วยการสร้างระดับสูงขั้นพื้นฐานสำหรับการเขียนโปรแกรมการแข่งขันใน Python ด้วย RapidKode คุณสามารถดำเนินการอัลกอริทึมที่ซับซ้อนในเวลาที่น้อยลงอัลกอริทึมทั้งหมดได้รับการปรับให้เหมาะสมที่สุดเพื่อลดความซับซ้อนของเวลาเพื่อให้คุณโดดเด่นในกระดานผู้นำ ไม่มีเวลาที่จะเสียเวลาเขียนรหัสขนาดใหญ่และดีบักในภายหลังด้วย RapidKode ทุกอย่างเกิดขึ้นที่ปลายนิ้วของคุณด้วยรหัสเดียว จุดมุ่งหมายของ RapidKode คือการช่วยให้ผู้เริ่มต้นเริ่มต้นในการเขียนโปรแกรมเข้าใจถึงความสำคัญของเวลาและพื้นที่ คำขวัญของการทำ Rapidkode คือ 'ทำให้ถูกต้องในครั้งแรก' แทนที่จะใช้เวลา 10 นาทีอันมีค่าในฟังก์ชั่น UTIL
การติดตั้ง:
หรือ
สำหรับปัญหารายงานข้อผิดพลาดและการบริจาคเยี่ยมชมการพัฒนา repo -> คลิกที่นี่
ฟังก์ชั่นที่มีอยู่:
ฟังก์ชั่นตัวเลข:
| ไวยากรณ์ | การดำเนินการ |
|---|
| numbers.gen_sparsenum_upto (x) | สร้างจำนวนกระจัดกระจายจนถึงช่วงที่กำหนด |
| numbers.get_sparsenum_after (n) | ส่งคืนหมายเลขเบาบางที่ประสบความสำเร็จสำหรับหมายเลขที่กำหนด |
| numbers.checkprime (x) | ส่งคืนจริงถ้าหมายเลขเป็นนายก |
| number.getPrimes.generate (x) | ส่งคืนตัวเลข X Prime First First |
| number.getPrimes.upto (x) | ส่งคืนตัวเลขที่สำคัญจนถึงช่วงที่กำหนด |
| number.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 จาก SYS เป็น SYS ใหม่ |
| ตัวอย่าง: | |
| แปลง (9845, 'dec'). เป็น ('bin') | แปลง 9845 จากทศนิยมเป็นไบนารี |
| แปลง (3745, 'OCT'). เป็น ('Hex') | แปลง 3745 จาก octal เป็น hexadecimal |
- คุณสามารถแทนที่ด้วย
sys ด้วย ['bin', 'dec', 'oct', 'hex'] และ new_sys ด้วย ['bin', 'dec', 'Oct', 'hex'] เพื่อทำการแปลงตัวเลข
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) |
| การค้นหาการแก้ไข | edpolation.search (arr, x) | ส่งคืนตำแหน่งของ x ใน arr | o (log2 (log2 n)) |
| การค้นหาแบบทวีคูณ | exponential.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) |
| เรียงลำดับ | insertion.sort (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) |
| การจัดเรียงนกพิราบ | 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 ([]) | สร้างกราฟที่มีรายการ coord ที่กำหนด |
| .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 ซึ่งเป็นอัลกอริทึมที่ใช้สำหรับการค้นหา/การจับคู่รูปแบบในข้อความโดยใช้ฟังก์ชันแฮช ซึ่งแตกต่างจากอัลกอริทึมการจับคู่สตริงไร้เดียงสามันไม่ได้เดินทางผ่านตัวละครทุกตัวในเฟสเริ่มต้น แต่จะกรองอักขระที่ไม่ตรงกันและทำการเปรียบเทียบ
| ไวยากรณ์ | การดำเนินการ |
|---|
| pattern.ithere (a) .inn (b) | ส่งคืนจริงถ้าสตริง A อยู่ในสตริง B |
| pattern.whereis (a) .inn (b) | ส่งคืนตำแหน่งดัชนีของสตริง a ในสตริง b |
ตัวอย่าง:
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, 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']
การจัดการบิต fuctions:
| ไวยากรณ์ | การดำเนินการ |
|---|
| bits.toggle_bits (x) | สลับบิตชุดและชุดที่ไม่ตั้งค่า |
| bits.convert_to_bin (x) | แปลงหมายเลขที่กำหนดเป็นไบนารี |
| bits.counsetBits (x) | ส่งคืนเลขที่ชุดบิตในหมายเลขธันวาคม |
| bits.rotate_byleft (x, d) | หมุนบิตไปทางซ้ายด้วยเวลา D |
| bits.rotate_byright (x, d) | หมุนบิตไปทางซ้ายด้วยเวลา D |
| bits.countflips (x, y) | ส่งคืนเลขที่พลิกเพื่อทำให้ 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
- การมีส่วนร่วมทั้งหมดรายงานข้อผิดพลาดการแก้ไขข้อผิดพลาดการปรับปรุงเอกสารการปรับปรุงและความคิดยินดีต้อนรับ
- ยกปัญหาหากคุณพบปัญหาใด ๆ
- หากคุณต้องการมีส่วนร่วมส้อม repo ยกปัญหาก่อนทำการร้องขอการดึงมันจะง่ายสำหรับการจัดการ
- เครดิตโลโก้และส่วนหัว -> M.Sri Harsha❤
มีความสุขอย่างรวดเร็ว Koding !!