I am in a coding bootcamp so I am really brand new to all of this and I was wondering why when I run this code it will only print the first two spots of the array?
words = ["Hello", "Goodbye", "Yes", "No"]
index = 0.to_i
while index.to_i < words.length
p words[index.to_i]
index = index.to_s + 1.to_s
end
It's because of
index = index.to_s + 1.to_s
to_s converts the receiver to a string, i.e. 0 becomes "0" and 1 becomes "1". Calling + on strings concatenates them, i.e. "0" + "1" becomes "01".
That one is fine, because "01".to_i is still 1. However, on the next iteration you get "01" + "1" which becomes "011" and "011".to_i is 11 which is more than the array's length.
To fix your code, you just have to remove the conversion and stick to integers:
words = ["Hello", "Goodbye", "Yes", "No"]
index = 0
while index < words.length
p words[index]
index = index + 1
end
You can also let Ruby handle the index for you via each_index:
words.each_index do |index|
p words[index]
end
or without an index via each:
words.each do |word|
p word
end
I don't know Ruby so I'm not sure about syntax.
But I can solve your logic.
words = ["Hello", "Goodbye", "Yes", "No"]
index = 0.to_i
while index.to_i < words.length
p words[index.to_i]
index = index.to_i + 1
end
This shall work.
Logic is this :
while index is less than length keep looping
print words[index]
increment index by 1 so that it moves to next position.
Related
I am working on an assignment where I have to take user input of a string and search through it to find the longest non-repeating string in it. So for example:
If the string is:
"abcabcabcdef"
My output needs to be:
"abcdef is the longest substring at the value of 6 characters"
Here is my poorly made code:
class Homework_4
puts "Enter any string of alphabetical characters: "
user_input = gets
longest_str = 0
empty_string = ""
map = {}
i = 0
j = 0
def long_substr()
while j < str_len
if map.key?(user_input[j])
i = [map[user_input[j]], i].max
end
longest_str = [longest_str, j - i + 1].max
map[user_input[j]] = j + 1
j += 1
end
longest_str
end
long_substr(user_input)
end
I have been working on this for over 6 hours today and I just can't figure it out. It seems like the internet has many ways to do it. Almost all of them confuse me greatly and don't really explain what they're doing. I don't understand the syntax they use or any of the variables or conditions.
All I understand is that I need to create two indicators that go through the inputted string searching for a non-repeating substring (sliding window method). I don't understand how to create them, what to make them do or even how to make them find and build the longest substring. It is very confusing to try and read the code that is full of random letters, symbols, and conditions. I'm sure my code is all sorts of messed up but any help or tips that could point me in the right direction would be greatly appreciated!
def uniq?(s)
# All letters of s uniq?
return s.chars.uniq == s.chars
end
def subs(s)
# Return all substrings in s.
(0..s.length).inject([]){|ai,i|
(i..s.length - i).inject(ai){|aj,j|
aj << s[i,j]
}
}.uniq
end
def longest_usub(s)
# Return first longest substring of s.
substrings(s).inject{|res, s| (uniq?(s) and s.length > res.length) ? s : res}
end
ruby's inject is actually a reduce function, where inject(optional_start_value){<lambda expression>} - and the lambda expression is similar to Python's lambda x, y: <return expression using x and y> just that lambda expressions are strangely written in Ruby as {|x, y| <return expression using x and y>}.
Python's range(i, y) is Ruby's i..y.
Python's slicing s[i:j] is in Ruby s[i..j] or s[i,j].
<< means add to end of the array.
Second solution (inspired by #Rajagopalan's answer)
def usub(s)
# Return first chunk of uniq substring in s
arr = []
s.chars do |char|
break if arr.include? char
arr << char
end
arr.join
end
def usubs(s)
# Return each position's usub() in s
(0..s.length).to_a.map{|i| usub(s[i,s.length])}
end
def longest_usub(s)
# return the longest one of the usubs() over s
usubs(s).max_by(&:length)
end
then you can do:
longest_usub("abcabcabcdef")
## "abcdef"
I have asssumed that a string is defined to be repeating if it contains a substring s of one or one more characters that is followed by the same substring s, and that a string is non-repeating if it is not repeating.
A string is seen to be repeating if and only if it matches the regular expression
R = /([a-z]+)\1/
Demo
The regular expression reads, "match one or more letters that are saved to capture group one, then match the content of capture group 1".
For convenience we can construct a simple helper method.
def nonrepeating?(str)
!str.match? R
end
I will perform a binary search to find the longest non-repeating string. First, I need a second helper method:
def find_nonrepeating(str, len)
0.upto(str.size-len) do |i|
s = str[i,len]
return s if nonrepeating?(s)
end
nil
end
find_nonrepeating("abababc", 7) #=> nil
find_nonrepeating("abababc", 6) #=> nil
find_nonrepeating("abababc", 5) #=> nil
find_nonrepeating("abababc", 4) #=> "babc"
find_nonrepeating("abababc", 3) #=> "aba"
find_nonrepeating("abababc", 2) #=> "ab"
find_nonrepeating("abababc", 1) #=> "a"
We may now implement the binary search.
def longest(str)
longest = ''
low = 0
high = str.size - 1
while low < high
mid = (low + high)/2
s = find_nonrepeating(str, mid)
if s
longest = s
low = mid + 1
else
high = mid - 1
end
end
longest
end
longest("dabcabcdef")
#=> "bcabcdef"
a = "abcabcabcdef"
arr = []
words = []
b=a
a.length.times do
b.chars.each do |char|
break if arr.include? char
arr << char
end
words << arr.join
arr.clear
b=b.chars.drop(1).join
end
p words.map(&:chars).max_by(&:length).join
Output
"abcdef"
I need to capitalize every 'nth' character for each word in a string (every multiple of 4-th character in this example, so character 4, 8, 12 etc).
I came up with the code below (not very elegant I know!) but it only works for words which length < 8.
'capitalize every fourth character in this string'.split(' ').map do |word|
word.split('').map.with_index do |l,idx|
idx % 3 == 0 && idx > 0 ? word[idx].upcase : l
end
.join('')
end
.flatten.join(' ')
Anybody could show me how to capitalize every 4th character in words which length > 8?
Thanks!
str = 'capitalize every fourth character in this string'
idx = 0
str.gsub(/./) do |c|
case c
when ' '
idx = 0
c
else
idx += 1
(idx % 4).zero? ? c.upcase : c
end
end
#=> "capItalIze eveRy fouRth chaRactEr in thiS strIng"
As an option, you can just modify the nth character in the string if it exists by accessing the character by index:
'capitalizinga every fourth character in this string'.split(' ').map do |word|
(3..word.length).step(4) do |x|
c = word[x]
word[x] = c.upcase if c
end
word
end.join(' ')
# capItalIzinGa eveRy fouRth chaRactEr in thiS strIng
Here is the method step or Range class is used, so each fourth index could be calculated: 3, 7, 11, etc...
I think the easiest way is to use a regex with substitution:
'capitalize every fourth character in this string'
.gsub(/([\w]{3})(\w)|([\w]{1,3})/) {
"#{$1}#{$2.to_s.upcase}#{$3}"
}
# => capItalIze eveRy fouRth chaRactEr in thiS strIng
This uses 2 alternatives with captured groups - the first alternative matches 4 characters and the second everything with 1 to 3 characters. Group $1 will match exactly three letters and group $2 the fourth letter within a 4-letter block - while group $3 will match remainders of a longer word as well words shorter than 4 characters.
You can then replace group $2 globally with gsub. Also you need to do $2.to_s in case $2 is nil (or catch that scenario with a ternary operator).
You can inspect the regex here and try the code here
> str.split(" ").map{|word|
word.chars.each_with_index{|c,i|
c.upcase! if (i > 0 && (i+1)%4 == 0)}.join}.join(" ")
#=> "capItalIze eveRy fouRth chaRactEr in thiS strIng"
def capitalize_each_nth_char(str, n)
str.chars.each_slice(n).to_a.each { |arr| arr[-1] = arr[-1].upcase if arr.size == n }.join('')
end
Here is the explanation,
str.chars # will give array of characters
str.chars.each_slice(n) # will give an enumerator as, #<Enumerator: ...>
str.chars.each_slice(n).to_a # will give an array of arrays
arr[-1].upcase # it will capitalize the last element i.e. 4th element of each array
if arr.size == n # it will prevent to capitalize last element of sub-array if it's size is less than n(in our case 4)
str.chars.each_slice(n).to_a.each { |arr| arr[-1] = arr[-1].upcase if arr.size == n } # it will give array of subarray where every subarray last element is capital
str.chars.each_slice(n).to_a.each { |arr| arr[-1] = arr[-1].upcase if arr.size == n }.join('') # it will give the final result as, "capItalIze EverY foUrth chaRactEr iN thIs sTrinG"
I am supposed to return the size of the largest palindrome within a given string. For example, if I pass "racecar", I should get a return of 7. If I pass "racecarveryfast" or "veryfastracecar", it should still return 7. Specs I have to pass are:
Test.assert_equals(longest_palindrome("a"), 1)
Test.assert_equals(longest_palindrome("aa"), 2)
Test.assert_equals(longest_palindrome("baa"), 2)
Test.assert_equals(longest_palindrome("aab"), 2)
Test.assert_equals(longest_palindrome("baabcd"), 4)
Test.assert_equals(longest_palindrome("baablkj12345432133d"), 9)
and I am passing the first four with this code:
def longest_palindrome s
sub_count = 0
palidrome_count = []
s_array = s.chars
puts "string: " + s
puts "string array: " + s_array.to_s
if s.reverse == s
return s.size
else
s.match('(.)\1')[0].size
end
end
My thought process from here is breaking apart the string into smaller chunks, maybe through a loop. Any help or guidance would be appreciated.
def longest_palindrome(string)
i = 0
a = []
while !string[i..-1].empty?
j = -1
while !string[i..j].empty?
s = string[i..j]
if s.reverse == s
a << s.length
end
j -= 1
end
i += 1
end
a.max
end
Suppose the string has n characters. First see if the entire string is a palindrome. If it is, return the string. Fini! If not, see if either of the two substrings of length n-1 is a palindrome. If one is, return it. If not, examine substrings of length n-2, and so on. As long as the string contains at least one letter, the longest palindrome will be found.
def longest_palindrome(str)
arr = str.downcase.chars
str.length.downto(1) do |n|
ana = arr.each_cons(n).find { |b| b == b.reverse }
return ana.join if ana
end
end
The key method here is Enumerable#each_cons.
Here are some examples1:
longest_palindrome "a" #=> "a"
longest_palindrome "aa" #=> "aa"
longest_palindrome "baa" #=> "aa"
longest_palindrome "aab" #=> "aa"
longest_palindrome "baabcd" #=> "baab"
longest_palindrome "baablkj12345432133d" #=> "123454321"
longest_palindrome "I heard tattarrattats" #=> "tattarrattat"
1 James Joyce coined the word "tattarrattat" in Ulysses, to mean a knock on the door.
I've been stuck on this Codeacademy exercise for the last 45 minutes or so. None of the proposed solutions from the Q&A forum worked. The code is as follows.
puts "Type something profound please"
text = gets.chomp
words = text.split
frequencies = Hash.new 0
frequencies = frequencies.sort_by {|x,y| y}
words.each {|word| frequencies[word] += 1}
frequencies = frequencies.sort_by{|x,y| y}.reverse
puts word +" " + frequencies.to_s
frequencies.each do |word, frequencies|
end
Why can't it convert the string into an integer? What am I doing incorrectly?
sort_by { |obj| block } → array is clear.
frequencies.sort_by {|x,y| y} gives you an array. frequencies is an array, not a hash. Thus in words.each {|word| frequencies[word] += 1}, frequencies[word] raises an exception, as frequencies is an array, and elements of an array is accessed via the integer index, but you tried with string word. words = text.split, so words holds array of strings.
I'll try to add a concrete example to help out here.
puts "Type something profound please"
text = gets.chomp
words = text.split
frequencies = {}
In Ruby, it is more typical to declare a hash with {} than with Hash.new, but either works.
words.each do |word|
if frequencies.has_key?(word)
frequencies[word] += 1
else
frequencies[word] = 1
end
end
The above simply iterates over the words array to build out the frequencies hash (hash[key] = value). It can also be abbreviated as:
words.each {|word| frequencies.has_key?(word) ? frequencies[word] += 1 : frequencies[word] = 1 }
Also, it is helpful if you provide your desired output/return value. I'm assuming you're wanting a hash of word => integer (as shown below), but this could easily be tweaked if you want some other format or sorting.
{
"my" => 1,
"array" => 1,
"of" => 1,
"words" => 2,
"with" => 1,
"some" => 1,
"repeating" => 1
}
I don't understand why, but I'm getting too many inserts and matches generated when I nest these two loops. Any help appreciated!
pseudocode
two arrays - nested for loops
search 2nd array for match of each element in 1st array
if there is a match in 2nd array, take the number after the match
insert number in 1st array after word that has been matched
end
problem code:
ary1 = ['a','b','c','d']
ary2 = ['e','f','g', 'a']
limit = ary1.count - 1
limit2 = ary2.count - 1
(0..limit).each do |i|
(0..limit2).each do |j|
if ary1[i] == ary2[j]
ary1.insert(i,ary2[j])
puts 'match!'
end
end
end
puts ary1
output:
match!
match!
match!
match!
a
a
a
a
a
b
c
d
provisional solution:
ary1 = ['a','b','c','d']
ary2 = ['e','f','g', 'a']
# have to make a copy to avoid excessive matches
ary_dup = Array.new(ary1)
limit = ary1.count - 1
limit2 = ary2.count - 1
(0..limit).each do |i|
(0..limit2).each do |j|
if ary1[i] == ary2[j]
ary_dup.insert(i,ary2[j])
puts 'match!'
end
end
end
puts ary_dup
output:
match!
a
a
b
c
d
Its happening because you're modifying array (ary1) under examination on the fly.
You could achieve desired result using this line of code -
(ary1 & ary2).each {|e| ary1.insert(ary1.index(e)+1,e)}
What it does is -
ary1 & ary2 returns an array which is intersection of two arrays - ary1 and ary2. In other words it'll contain all those elements that exist in both arrays.
.each and ensuing block traverses over this new array and inserts each element in ary1 at "index of original element" + 1
puts ary1 #=> ["a", "a", "b", "c", "d"]
The below part is not correcrt:
(0..limit).each do |i|
(0..limit2).each do |j|
if ary1[i] == ary2[j]
ary1.insert(i,ary2[j])
puts 'match!'
end
end
end
First pass:
ary1 = ['a','b','c','d']
ary2 = ['e','f','g', 'a']
when limit=0 and limit2 = 3,there is a match.ary1.insert(0,ary2[j]) line makes your array ary1 as ary1 = ['a','a','b','c','d']
Second pass:
ary1 = ['a','a',b','c','d']
ary2 = ['e','f','g', 'a']
when limit=1 and limit2 = 3,there is a match.ary1.insert(1,ary2[j]) line makes your array ary1 as ary1 = ['a','a','a','b','c','d'].
And it Goes on.. So as your arr1is having size 4, 4 a s has been added to ary1. Finally it becomes - [ a,a,a,a,a,b,c,d].
Array#insert says :-
Inserts the given values before the element with the given index.Negative indices count backwards from the end of the array, where -1 is the last element.