mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-07 21:04:02 +08:00
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
43 lines
1.1 KiB
Ruby
43 lines
1.1 KiB
Ruby
=begin
|
|
File: deque.rb
|
|
Created Time: 2024-04-06
|
|
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|
=end
|
|
|
|
### Driver Code ###
|
|
if __FILE__ == $0
|
|
# Get the length of the double-ended queue
|
|
# Ruby has no built-in deque, can only use Array as deque
|
|
deque = []
|
|
|
|
# Element enqueues
|
|
deque << 2
|
|
deque << 5
|
|
deque << 4
|
|
# Note: due to array, Array#unshift method has O(n) time complexity
|
|
deque.unshift(3)
|
|
deque.unshift(1)
|
|
puts "Deque deque = #{deque}"
|
|
|
|
# Update element
|
|
peek_first = deque.first
|
|
puts "Front element peek_first = #{peek_first}"
|
|
peek_last = deque.last
|
|
puts "Rear element peek_last = #{peek_last}"
|
|
|
|
# Element dequeue
|
|
# Note: due to array, Array#shift method has O(n) time complexity
|
|
pop_front = deque.shift
|
|
puts "Dequeue front element pop_front = #{pop_front}, after dequeue deque = #{deque}"
|
|
pop_back = deque.pop
|
|
puts "Dequeue rear element pop_back = #{pop_back}, after dequeue deque = #{deque}"
|
|
|
|
# Get the length of the double-ended queue
|
|
size = deque.length
|
|
puts "Deque length size = #{size}"
|
|
|
|
# Check if the double-ended queue is empty
|
|
is_empty = size.zero?
|
|
puts "Is deque empty = #{is_empty}"
|
|
end
|