Replace the second integer/number in a string - ruby

Given different Strings with each two Integers, what is the easiest way in Ruby (2.7) to replace the second Integer?
Example:
'We are there from 2 to 5 oclock'
'You can stack 15 onto 22 boxes'
So far I am using string::gsup but I am wondering if there is a better way?

gsub is the easiest way to achieve this job.
Input
a='We are there from 2 to 5 oclock'
Code
p a.gsub(/\D*\d+\D+\K\d+/,"10000")
Output
"We are there from 2 to 10000 oclock"

Related

how to display 3 random pictures out of 8 using bash

I have 8 images in a directory.
the path is /blabla.com/img.
I need to access this path and choose 3 out of 8 randomly and display those.
If 3 pics are the same, it should echo "yeeey".
Otherwise, "neeey" and record these responses in a text file.
I am not going to do your homework for you!
However I can give you some insight:
store your 8 file names in an array
call $RANDOM % 8 3 times and store the value in 3 index variables
use the 3 index variables to extract your 3 files
use sha256sum, sha512sum or md5sum to compute the signature of your images and store the result in 3 variables
compare the values of the 3 variables if they are the same echo "yeeey" else echo "neeey"
if on top of that you want to display the picture as written in your post you could call eog or other similar tool with the finename as parameter and of course in background, with a & at the end of the command call.
Good luck with your assignment and let me know if you need help!
let's array an array of distinct elements (for example 8):
array=({A..H})
(1) use RANDOM special variable modulo the number of elements to get a random number between 0 and number-1 inclusive
number=$((RANDOM%${#array[#]}))
the first random element is
first=${array[number]}
remove the element from array and reassign the array to reindex without gap (declare -p array to see)
unset array[number]
array=("${array[#]}")
restart from (1)

Get an item randomly from a list

I've made a simple guessing game, which utilizes simple if-statement. Guess a number between 0 and 10. It'll hint you to put a higher/lower number. That's working fine. To make it a little more interesting, I'd like to taunt the user until the right answer is input.
Question: How to get a message item randonly and return this message if the user has not guessed the right number?
Example:
Guess a number: 5
You have no idea, huh? Try higher. 7
Ha! still far from it. Try higher. 8
Did you just fart? I'm sure it was a perfect 10! Try higher. 10
You momma did teach you how to count, right? Try lower. 9
Yes, well done!
Any ideas? :)
You can try:
# Store your string in a BASH array:
arr=( "foo" "bar" "baz" "abc" "xyz" )
# get a number between 0 and length of array:
len=${#arr[#]}
# get a random string from array using BASH variable $RANDOM
n=$(($RANDOM % len))
echo ${arr[$n]}

Basic for loop in Python 3

I'm a beginner to Python and I'm having some trouble with this. I have to make a for loop out of this problem. Can anyone explain how I would go about this?
nextNValues (startValue, increment, numberOfValues)
This function creates a string of numberOfValues values, starting with startValue and
counting by increment. For example, nextNValues (5, 4, 3) would generate a string of
(not including the comments):
5 - the start value
9 - counting by 4, the increment
13 - stopping after 3 lines of output, the numberOfValues
You could use range(startValue,startValue+(increment*numberofValues),increment).
for i in range(numberOfValues):
print startValue + i * increment
I am not sure if that is exactly what you are looking for... but it is my suggestion based on the information you have posted.
It would probably be easiest to write a for loop with an index like i and use that to add i*increment ti start value and save the resulting value to a list. Have the loop run numberOfValues times. If this is homework it would be better for you to write out the actual code for yourself

How do I create random in Eclipse?

I have 10 images and I want them to be prompt out randomly. What code should I use? I'm still an amateur so I hope I could get some answers here... The images are named like 'pic1', 'pic2' and so on. is it possible to get the number from the file name and use Math.random() ?
Store the pics in an Array.
Say array index is from 0 to 9
Use Java Math.random to get random number.
You need to multiply its result by 10 get indexes in your range

Parsing text files in Ruby when the content isn't well formed

I'm trying to read files and create a hashmap of the contents, but I'm having trouble at the parsing step. An example of the text file is
put 3
returns 3
between
3
pargraphs 1
4
3
#foo 18
****** 2
The word becomes the key and the number is the value. Notice that the spacing is fairly erratic. The word isn't always a word (which doesn't get picked up by /\w+/) and the number associated with that word isn't always on the same line. This is why I'm calling it not well-formed. If there were one word and one number on one line, I could just split it, but unfortunately, this isn't the case. I'm trying to create a hashmap like this.
{"put"=>3, "#foo"=>18, "returns"=>3, "paragraphs"=>1, "******"=>2, "4"=>3, "between"=>3}
Coming from Java, it's fairly easy. Using Scanner I could just use scanner.next() for the next key and scanner.nextInt() for the number associated with it. I'm not quite sure how to do this in Ruby when it seems I have to use regular expressions for everything.
I'd recommend just using split, as in:
h = Hash[*s.split]
where s is your text (eg s = open('filename').read. Believe it or not, this will give you precisely what you're after.
EDIT: I realized you wanted the values as integers. You can add that as follows:
h.each{|k,v| h[k] = v.to_i}

Resources