This question already has an answer here:
Difference between String.scan and String.split
(1 answer)
Closed 9 years ago.
What is the difference between ".scan" and ".split" in Ruby language?
Can someone give me an example?
i am really got confused about it!
scan extracts substrings that match the given regex.
split extracts substrings that do not match the given regex (and optionally, substrings that match as well).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Design an algorithm to determine how many numbers are in a string. Example, given the string "Hello people from the 4 worlds, this is my only 1 program", the output must be 2.
Basically you need to write a simple parser to parse out the numbers in your string. To do that you need to be able to recognise a number correctly, which is a little more complicated than just recognising digits. Something like "-12,348.971" is a number, but contains the characters -,. which are not digits. However, the string "-,." is not itself a number.
Read through the string, character by character. When the parser finds the start of a number, count one more number found, and read through all the characters that form that number. Read '123' as a single number, not three numbers. When you reach the end of the number skip over non-number characters until either you find the next number or you reach the end of the file.
You might want to read up on writing a simple parser in the language of your choice.
This question already has answers here:
Quick Sort in Ruby language
(6 answers)
Closed 7 years ago.
I'm having trouble wrapping my head around a in-place quick sort. I understand it using sub-arrays, but in-place is really throwing me.
Having an example in Ruby would really help, but I haven been able to find one. Could some one provide me with an example or point me in the right direction?
Rather than trying to understand it in code, there's a number of very elegant explanations of the quicksort algorithm out there. Here are a few of my favorite illustrations which may help your understanding.
With cups
With Hungarian Dance
As for a Ruby example, the answers to this question cover that.
This question already has answers here:
How do I pick randomly from an array?
(7 answers)
Closed 8 years ago.
I am trying to understand how to use rand to do this. if there were two players for example player a and player b and the computer had to be one and the human the other. How would i implement this?
[:computer, :player].shuffle.first will give you on random who is first.
This question already has answers here:
In ruby how do you tell if a string input is in uppercase or lowercase?
(3 answers)
Closed 8 years ago.
How to detect in Ruby if a sentence has all lower case letters? Likewise, how to detect if a sentence has all uppercase letters?
You can assume this is an English sentence with no foreign characters.
Simple question, I know. Please excuse brevity.
'string' == 'string'.downcase
If the string is equal to the string with all letters lowercased, then all letters are lowercased.
See here and here.
This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 9 years ago.
I was recently asked this question in an interview.
Initially, I was asked to reverse a sentence without using inbuilt String api methods like split etc.
I/p : I like god
O/p : god like I
I did this using a stack. His next question was to accomplish this without using additional memory.
How do we achieve this in java?
Thanks!
Reverse the sentence in-place on a character-by-character basis, then reverse each word in the sentence in-place on a character-by-character basis.