Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Ruby is a multi-platform, open-source, dynamic, object-oriented, interpreted language created by Yukihiro Matsumoto (Matz) in 1993.
0
votes
Determines whether a number is a power of 2
You also can convert the number to binary string form and match it with a regex:
!!num.to_s(2)[/^10*$/]
2
votes
GCD of two numbers in Ruby
def gcd a, b
return a if b.zero?
gcd b, a % b
end
Avoid ?: when the <return/next/break> if <condition> form can be used.
5
votes
Luhn algorithm in Ruby
As a small improvement on top the other answer, you could avoid declaring a double variable by using .divmod to split the digits, like this:
def luhn card_no
return false if card_no.empty?
card_n …
1
vote
Accepted
Sum of numbers that equal 100
first, :+)
end
end
end
end
end
0.0013sec vs 2.0sec
Making it recursive (break on any depth) could achieve the optimal speed, but such solution becomes not that so easy to read as Ruby …
4
votes
Sort array of hashes with nil values last
You can create temporary sorting columns:
def sort records, *attrs
records.sort_by do |h|
h.values_at(*attrs).map do |v|
v.nil? ? [2] : [1, v]
end
end
end
Here I added columns wit …
5
votes
Accepted
Balanced parenthesis in Ruby
To eliminate nested loop you may apply regex checking for invalid symbol from the start -- it would be O(n) + O(n) = O(n):
def balanced? string
return false if string.length.odd?
return false if …
0
votes
Finding the number in a list that differs in parity
There are three stop conditions depending on the oddity of gaps between the 3rd, 2nd, and 1st most recent number:
0 1 -> 1st (the last one) is the answer
1 0 -> 3rd
1 1 -> 2nd
You can run through …
2
votes
Accepted
Counting numbers whose digits all go up or down
To make Ruby internally use a loop instead of an Array you may want to use 1.upto(10 ** x) that is an Enumerator.
But I doubt the "HUGE array" is the performance issue here. … First of all Ruby says to do total = .count{ ... } instead of total = 0; .each{ total += 1 if ... }
Then do this thing only once and store in a variable t = i.to_s.split("").sort -- x2 faster
Then use …
3
votes
Generate word list based on Spanish text file
Based on @tokland code.
No need to join array with newline symbol -- #puts does that for you silently:
File.open("quijote-words.txt", "w") do |file|
file.puts File.read("quijote.txt").
downcase …
1
vote
Accepted
Destroy script for provisioning
Use Kernel#abort:
puts "[#{$me}] ERROR: host to destroy must be in first argument. Exit."
exit 1
will behave the same as:
abort "[#{$me}] ERROR: host to destroy must be in first argument. Exit."
…
1
vote
Balancing words
You may use splat operator instead of .to_a
You may use .map.with_index instead of .each_with_index.map
You should (bug?) use ... instead of ..
def balanced_at? string, index, alphabet = [*"a".."z" …
3
votes
Accepted
OO coding style in short Ruby scripts
Never use OOP just because people say it's cool. If you do not know how to apply it, that means you just do not need it in this task. That's it. Forget about OOP. Involving it without much need only m …
0
votes
Checking whether two strings are isomorphic
I do not match Code Review with this a lot, but I found this problem to be interesting and would solve it without .zip and {}:
def isomorphic? a, b
each_char_positions = lambda do |string|
str …
1
vote
Lottery number generator
Using Set instead of Array is faster and checks for presence for you:
require "set"
def get_random_numbers count, min, max
set = Set.new
set << rand(min..max) until set.size == count
set.to_a
…
1
vote
Finding powers of multiple array pairs
Array#zip accepts block, so:
def fast_power a, b
a.zip(b){ |i,j| puts (i**j) % 4211 }
end
or
def fast_power a, b
puts a.zip(b).map{ |i,j| (i**j) % 4211 }
end
But I believe, that competit …