Ruby return matching character size - ruby

I'm trying to make a game which involves it returning the amount of characters that you've guessed that are in the correct position of the randomly generated word (eg., word is "board", and you enter "boat", you'd get 2/5, and you entered "board", you'd get 5/5).
word = File.readlines("wordslist.txt").sample;
guess = gets
same = guess.each_char.zip(word.each_char).select{ |g,w| g == w }.size
It works fine for any guess under the word length. If the word was "bye" and I entered "byk" it would return 3/3, but if I entered "by" it would return 2/3. Just looking to see if I'm doing anything wrong.

This is happening because both File.readlines and gets do not trim the trailing newline character from the returned string.
irb(main):001:0> File.read("wordslist.txt")
=> "hello\nbye\n"
irb(main):002:0> File.readlines("wordslist.txt")
=> ["hello\n", "bye\n"]
irb(main):003:0> gets
bye
=> "bye\n"
When your dictionary contains "bye\n" and you type "byk\n", there are actually 3 matches, "b", "y", and "\n". If you enter "by\n" though, the newline character doesn't match. The newline character will only match if the input strings are of the same length, and the returned value would be 1 more than you'd expect.
To fix this, you can call .chomp on both the strings to remove the trailing whitespace before comparing the characters:
word = File.readlines("wordslist.txt").sample.chomp;
guess = gets.chomp
same = guess.each_char.zip(word.each_char).select{ |g,w| g == w }.size
Tip: You can use .count instead of .select and .size:
same = guess.each_char.zip(word.each_char).count{ |g,w| g == w }

def number_same_position(guess, word)
(0..word.size-1).count { |i| guess[i] == word[i] }
end
number_same_position("boat", "board") #=> 3
number_same_position("bait", "board") #=> 1
number_same_position("sulk", "board") #=> 0
number_same_position("boater", "board") #=> 3
number_same_position("", "board") #=> 0
One could instead use three dots ((0...word.size)) but I always use two dots. Recall string[i] #=> nil when i >= string.size.

Related

Increment alphabetical string in Ruby on rails

Task I want to solve:
Write a program that takes a string, will perform a transformation and return it.
For each of the letters of the parameter string switch it by the next one in alphabetical order.
'z' becomes 'a' and 'Z' becomes 'A'. Case remains unaffected.
def rotone(param_1)
a = ""
param_1.each_char do |x|
if x.count("a-zA-Z") > 0
a << x.succ
else
a << x
end
end
a
end
And I take this:
Input: "AkjhZ zLKIJz , 23y "
Expected Return Value: "BlkiA aMLJKa , 23z "
Return Value: "BlkiAA aaMLJKaa , 23z "
When iterators find 'z' or 'Z' it increment two times z -> aa or Z -> AA
input = "AkjhZ zLKIJz , 23y"
Code
p input.tr('a-yA-YzZ','b-zB-ZaA')
Output
"BlkiA aMLJKa , 23z"
Your problem is that String#succ (aka String#next) has been designed in a way that does not serve your purpose when the receiver is 'z' or 'Z':
'z'.succ #=> 'aa'
'Z'.succ #=> 'AA'
If you replaced a << x.succ with a << x.succ[0] you would obtain the desired result.
You might consider writing that as follows.
def rotone(param_1)
param_1.gsub(/./m) { |c| c.match?(/[a-z]/i) ? c.succ[0] : c }
end
String#gsub's argument is a regular expression that matches every character (so every character is passed to gsub's block)1.
See also String#match?. The regular expression /[a-z]/i matches every character that is one of the characters in the character class [a-z]. The option i makes the match case-independent, so uppercase letters are matched as well.
Here is alternative way to write the method that employs two hashes that are defined as constants.
CODE = [*'a'..'z', *'A'..'Z'].each_with_object({}) do |c,h|
h[c] = c.succ[0]
end.tap { |h| h.default_proc = proc { |_h,k| k } }
#=> {"a"=>"b", "b"=>"c",..., "y"=>"z", "z"=>"a",
# "A"=>"B", "B"=>"C",..., "Y"=>"Z", "Z"=>"A"}
DECODE = CODE.invert.tap { |h| h.default_proc = proc { |_h,k| k } }
#=> {"b"=>"a", "c"=>"b", ..., "z"=>"y", "a"=>"z",
# "B"=>"A", "C"=>"B", ..., "Z"=>"Y", "A"=>"Z"}
For example,
CODE['e'] #=> "f"
CODE['Z'] #=> "A"
CODE['?'] #=> "?"
DECODE['f'] #=> "e"
DECODE['A'] #=> "Z"
DECODE['?'] #=> "?"
Let's try using gsub, CODE and DECODE with an example string.
str = "The quick brown dog Zelda jumped over the lazy fox Arnie"
rts = str.gsub(/./m, CODE)
#=> "Uif rvjdl cspxo eph Afmeb kvnqfe pwfs uif mbaz gpy Bsojf"
rts.gsub(/./m, DECODE)
#=> "The quick brown dog Zelda jumped over the lazy fox Arnie"
See Hash#merge, Object#tap, Hash#default_proc=, Hash#invert and the form of Sting#gsub that takes a hash as its optional second argument.
Adding the default proc to the hash h causes h[k] to return k if h does not have a key k. Had CODE been defined without the default proc,
CODE = [*'a'..'z', *'A'..'Z'].each_with_object({}) { |c,h| h[c] = c.succ[0] }
#=> {"a"=>"b", "b"=>"c",..., "y"=>"z", "z"=>"a",
# "A"=>"B", "B"=>"C",..., "Y"=>"Z", "Z"=>"A"}
gsub would skip over characters that are not letters:
rts = str.gsub(/./m, CODE)
#=> "UifrvjdlcspxoephAfmebkvnqfepwfsuifmbazgpyBsojf"
Without the default proc we would have to write
rts = str.gsub(/./m) { |s| CODE.fetch(s, s) }
#=> "Uif rvjdl cspxo eph Afmeb kvnqfe pwfs uif mbaz gpy Bsojf"
See Hash#fetch.
1. The regular expression /./ matches every character other than line terminators. Adding the option m (/./m) causes . to match line terminators as well.

Counting all occurrences of digits in a string

You are given a string S. Count the number of occurrences of all the digits in the string S.
Input:
First line contains string S
Output:
Count the number of occurences of 1 in the entered number and print the output.
I tried to attempt the given problem like this:
number=$stdin.gets.chomp
number.split('').map(&:to_i)
number.each do |numbers|
i==0
while numbers===1
i+=1
end
end
puts i
But, it's not executing.
Can someone please suggest how may I do it?
A clean way to do what you want in ruby
number=$stdin.gets.chomp
number.count('1')
str = "1a2b &32T2*3"
You can count the total number of digits in the string like so:
str.count('0123456789')
#=> 6
If you wish to count the number of occurrences of each digit in the string, you could use a counting hash (see Hash::new):
str.each_char.with_object(Hash.new(0)) { |c,h| h[c] += 1 if c =~ /\d/ }
#=> {"1"=>1, "2"=>3, "3"=>2}
or use the method Enumerable#group_by:
h = str.each_char.group_by(&:itself).select { |k,_| k =~ /\d/ }
#=> {"1"=>["1"], "2"=>["2", "2", "2"], "3"=>["3", "3"]}
h.update(h) { |*,v| v.count }
#=> {"1"=>1, "2"=>3, "3"=>2}
This uses the form of Hash#update (aka merge!) that employs a block to determine the values of keys that are present in both hashes being merged (here all keys). See the doc for details.
There were several problems with your code. First you didn't actually set numbers to be an array, you can do that by saying numbers = numbers.split('').map(&:to_i)
Then you should declare the i variable outside of the loop. Instead of saying i == 0, use i = 0 for the assignment operator
Instead of using a while loop, you should use an if statement.
This code is assuming your string is only all numbers.
numbers = $stdin.gets.chomp
numbers = numbers.split('').map(&:to_i)
i = 0
numbers.each do |number|
if number === 1
i += 1
end
end
If your string consists of letters, numbers or special characters, you could do this
numbers = $stdin.gets.chomp
numbers.split('').select { |c| c=~ /1/ }.length

Ruby transform string of range measurements into a list of the measurements?

I have a sample string that I would like to transform, from this:
#21inch-#25inch
to this:
#21inch #22inch #23inch #24inch #25inch
Using Ruby, please show me how this can be done.
You can scan your string and working with range of strings:
numbers = "#21inch-#25inch".scan(/\d+/)
=> ["21", "25"]
Range.new(*numbers).map{ |s| "##{s}inch" }.join(" ")
=> "#21inch #22inch #23inch #24inch #25inch"
This solution working only if your string has a format like in your instance. For other cases you should write your own specific solution.
R = /
(\D*) # match zero or more non-digits in capture group 1
(\d+) # match one or more digits in capture group 2
([^\d-]+) # match on or more chars other the digits and hyphens in capture group 3
/x # free-spacing regex definition mode
def spin_out(str)
(prefix, first, units),(_, last, _) = str.scan(R)
(first..last).map { |s| "%s%s%s" % [prefix,s,units] }.join(' ')
end
spin_out "#21inch-#25inch"
#=> "#21inch #22inch #23inch #24inch #25inch"
spin_out "#45cm-#53cm"
#=> "#45cm #46cm #47cm #48cm #49cm #50cm #51cm #52cm #53cm"
spin_out "sz 45cm-sz 53cm"
#=> "sz 45cm sz 46cm sz 47cm sz 48cm sz 49cm sz 50cm sz 51cm sz 52cm sz 53cm"
spin_out "45cm-53cm"
#=> "45cm 46cm 47cm 48cm 49cm 50cm 51cm 52cm 53cm"
For str = "#21inch-#25inch", we obtain
(prefix, first, units),(_, last, _) = str.scan(R)
#=> [["#", "21", "inch"], ["-#", "25", "inch"]]
prefix
#=> "#"
first
#=> "21"
units
#=> "inch"
last
#=> "25"
The subsequent mapping is straightforward.
You can use a regex gsub with a block match replacement, like this:
string = "#21inch-#25inch"
new_string = string.gsub(/#\d+\w+-#\d+\w+/) do |match|
first_capture, last_capture = match.split("-")
first_num = first_capture.gsub(/\D+/, "").to_i
last_num = last_capture.gsub(/\D+/, "").to_i
pattern = first_capture.split(/\d+/)
(first_num..last_num).map {|num| pattern.join(num.to_s) }.join(" ")
end
puts "#{new_string}"
Running this will produce this output:
First: #21inch Last: #25inch
First num: 21 Last num: 25
Pattern: ["#", "inch"]
#21inch #22inch #23inch #24inch #25inch
The last line of output is the answer, and the previous lines show the progression of logic to get there.
This approach should work for other, slightly different unit formats, as well:
#32ft-#49ft
#1mm-5mm
#2acres-5acres
Making this suit multiple purposes will be quite simple. With a slight variation in the regex, you could also support a range format #21inch..#25inch:
/(#\d+\w+)[-.]+(#\d+\w+)/
Happy parsing!

Find if all letters in a string are unique

I need to know if all letters in a string are unique. For a string to be unique, a letter can only appear once. If all letters in a string are distinct, the string is unique. If one letter appears multiple times, the string is not unique.
"Cwm fjord veg balks nth pyx quiz."
# => All 26 letters are used only once. This is unique
"This is a string"
# => Not unique, i and s are used more than once
"two"
# => unique, each letter is shown only once
I tried writing a function that determines whether or not a string is unique.
def unique_characters(string)
for i in ('a'..'z')
if string.count(i) > 1
puts "This string is unique"
else
puts "This string is not unique"
end
end
unique_characters("String")
I receive the output
"This string is unique" 26 times.
Edit:
I would like to humbly apologize for including an incorrect example in my OP. I did some research, trying to find pangrams, and assumed that they would only contain 26 letters. I would also like to thank you guys for pointing out my error. After that, I went on wikipedia to find a perfect pangram (I wrongly thought the others were perfect).
Here is the link for reference purposes
http://en.wikipedia.org/wiki/List_of_pangrams#Perfect_pangrams_in_English_.2826_letters.29
Once again, my apologies.
s = "The quick brown fox jumps over the lazy dog."
.downcase
("a".."z").all?{|c| s.count(c) <= 1}
# => false
Another way to do it is:
s = "The quick brown fox jumps over the lazy dog."
(s.downcase !~ /([a-z]).*\1/)
# => false
I would solve this in two steps: 1) extract the letters 2) check if there are duplicates:
letters = string.scan(/[a-z]/i) # append .downcase to ignore case
letters.length == letters.uniq.length
Here is a method that does not convert the string to an array:
def dupless?(str)
str.downcase.each_char.with_object('') { |c,s|
c =~ /[a-z]/ && s.include?(c) ? (return false) : s << c }
true
end
dupless?("Cwm fjord veg balks nth pyx quiz.") #=> true
dupless?("This is a string.") #=> false
dupless?("two") #=> true
dupless?("Two tubs") #=> false
If you want to actually keep track of the duplicate characters:
def is_unique?(string)
# Remove whitespaces
string = string.gsub(/\s+/, "")
# Build a hash counting all occurences of each characters
h = Hash.new { |hash, key| hash[key] = 0 }
string.chars.each { |c| h[c] += 1 }
# An array containing all the repetitions
res = h.keep_if {|k, c| c > 1}.keys
if res.size == 0
puts "All #{string.size} characters are used only once. This is unique"
else
puts "Not unique #{res.join(', ')} are used more than once"
end
end
is_unique?("This is a string") # Not unique i, s are used more than once
is_unique?("two") # All 3 characters are used only once. This is unique
To check if a string is unique or not, you can try out this:
string_input.downcase.gsub(/[^a-z]/, '').split("").sort.join('') == ('a' .. 'z').to_a.join('')
This will return true, if all the characters in your string are unique and if they include all the 26 characters.
def has_uniq_letters?(str)
letters = str.gsub(/[^A-Za-z]/, '').chars
letters == letters.uniq
end
If this doesn't have to be case sensitive,
def has_uniq_letters?(str)
letters = str.downcase.gsub(/[^a-z]/, '').chars
letters == letters.uniq
end
In your example, you mentioned you wanted additional information about your string (list of unique characters, etc), so this example may also be useful to you.
# s = "Cwm fjord veg balks nth pyx quiz."
s = "This is a test string."
totals = Hash.new(0)
s.downcase.each_char { |c| totals[c] += 1 if ('a'..'z').cover?(c) }
duplicates, uniques = totals.partition { |k, v| v > 1 }
duplicates, uniques = Hash[duplicates], Hash[uniques]
# duplicates = {"t"=>4, "i"=>3, "s"=>4}
# uniques = {"h"=>1, "a"=>1, "e"=>1, "r"=>1, "n"=>1, "g"=>1}

Separating a code chunk into the main parts and the expected return parts

What is the best way to separate a code chunk (string) into its "main parts" and its "expected return parts"? Here are my definitions:
An expected return part is a line that matches /^[ \t]*#[ \t]*=>/ followed by zero or more consecutive lines that do not match /^[ \t]*#[ \t]*=>/ but match /[ \t]*#(?!\{)/.
A main part is any consecutive lines that is not an expected return part.
Main parts and expected return parts may appear multiple times in a code chunk.
Given a string of code chunk, I want to get an array of arrays, each of which includes a flag of whether it is an expected return part, and the string. What is the best way to do this? For example, given a string code whose content is:
def foo bar
"hello" if bar
end
#=> foo(true) == "hello"
#=> foo(false) == nil
a = (0..3).to_a
#=> a == [
# 0,
# 1,
# 2,
# 3
# ]
I would like a return that would be equivalent to this:
[[false, <<CHUNK1], [true <<CHUNK2], [true, <<CHUNK3], [false, <<CHUNK4], [true, <<CHUNK5]]
def foo bar
"hello" if bar
end
CHUNK1
#=> foo(true) == "hello"
CHUNK2
#=> foo(false) == nil
CHUNK3
a = (0..3).to_a
CHUNK4
#=> a == [
# 0,
# 1,
# 2,
# 3
# ]
CHUNK5
This regex should match all expected returns:
^([ \t]*#[ \t]*=>.+(?:\n[ \t]*#(?![ \t]*=>).+)*)
Extract and then replace all expected returns from your string with a separator. Then split your string by the separator and you will have all main parts.
Test it here: http://rubular.com/r/ZYjqPQND28
There is a slight problem with your definition pertaining to the regex /[ \t]*#(?!>\{)/, by which I am assuming you meant /[ \t]*#(?!=>)/, because otherwise
#=> foo(true) == "hello"
#=> foo(false) == nil
would count as one chunk
Another approach would be to use this regex (completely unoptimised):
^([ \t]*#[ \t]*=>.+(?:\n[ \t]*#(?![ \t]*=>).+)*|(?:[ \t]*(?!#[ \t]*=>).+\n)*)
to simply split it into chunks correctly, then do a relatively simple regex test on each chunk to see if it is an expected return or main part.

Resources