Sorting Lunch Items Stored as Hash Values [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Can you please help me figure this out?
Write the code necessary to print JUST the values of each key-value
pair in lunch_order. (Use puts instead of print so each value is
printed on its own line.)
Code
lunch_order =
{ "Ryan" => "wonton soup",
"Eric" => "hamburger",
"Jimmy" => "sandwich",
"Sasha" => "salad",
"Cole" => "taco"
}
Hint:
For example, you should print out
wanton soup
hamburger
and so on.
Meaning: each y-value should be on its own line
I have tried various syntax but can't seem to figure out what I am doing wrong. Can someone please explain the right way to approach this problem? I would greatly appreciate it.
My Solution
I figured out the answer: create a y_value array and then use each method on it. For example:
y_value = ["wonton soup", "hamburger", "sandwich", "salad", "taco"]
y_value.each { |x| puts "#{x}" }

A More Idiomatic Solution
Your solution doesn't sort the values. There are certainly many ways to solve this problem, but the following is both short and idiomatic:
lunch_order.values.sort.each { |lunch| puts lunch }
This will send the following to standard output:
hamburger
salad
sandwich
taco
wonton soup

Related

How Do I Assign Values of 2 or more Fields in VBScript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Hi sorry for a basic question on VBScript but after scouring the internet. In a If statement I am trying to assign values to 2 fields but when I run this code which is part of an application it does not error neither does it work so I suspect I am doing wrong. Can anyone point me in the right direction as I am trying to assign values to CustomerInformation.CodeObject.Customer and. CustomerInformation.CodeObject.Carrier
Function CustomerInformation_OnLoad()
if (SystemVariables.CodeObject.Company = "D" or SystemVariables.CodeObject.Company = "T") and DispatchNoteDetails.CodeObject.Area = "UK" then
if CustomerInformation.CodeObject.Customer = "AAE02" then
CustomerInformation.CodeObject.Carrier = "Customer collects" and CustomerInformation.CodeObject.CarrierURN = "AA Driver"
end if
end if
End Function
Hi thanks all for the tips.
The code worked when changing to the following:
Function CustomerInformation_OnLoad()
if (SystemVariables.CodeObject.Company = "D" or SystemVariables.CodeObject.Company = "T")then
if CustomerInformation.CodeObject.Customer = "AAE02" then
CustomerInformation.CodeObject.Carrier = "Customer collects"
CustomerInformation.CodeObject.CarrierURN = "AA Driver"
end if
end if
End Function
Thanks again for the tips.

(irb):4: warning: key "abc" is duplicated and overwritten on line 4 [closed]

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 5 years ago.
Improve this question
Rearrange the key values in the hash below to look like:
{"abc"=>"wqeq","dfg"=>"sadsada","qwe"=>"asdad","yui"=>"asdasd","abc"=>"weqqw","qwe"=>"assadsad","yui"=>"asd","dfg"=>"asdsad"}
{"abc"=>["wqeq","weqqw"] ...}
Thanks
The problem here is that assigning a key with the same value as a previous one overwrites it.
As far as I'm aware, there's no code-based solution to solve this; you just need to re-write it with arrays as the keys' values where they're duplicated.
{"abc" => ["wqeq", "weqqw"],
"dfg" => ["sadsada", "asdsad"],
"qwe" => ["asdad", "assadsad"],
"yui" => ["asdasd", "asd"] }
I guess that's the output you're looking for. If not, please add a little more info to the questions and I / others will be able to help out.
If you want to bring a gun to a knife fight, you can use compare_by_identity:
hash = {}
hash.compare_by_identity
hash["abc"] = "wqeq"
hash["abc".dup] = "weqqw"
# ... etc ...
puts hash # => { "abc" => "wqeq", "abc" => "weqqw" }
... and then process that. Feels kinda dirty to me though.

Random numbers in Ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to make n random numbers from -0.5 from to 0.5
and I made a function like this
def create_noise(n)
end
I found an implementation of this but i don't think this works
randoms = Set.new
loop
randoms << rand(max)
return randoms.to_a if randoms.size >= n
You would just do
def create_noise(n)
n.times.collect { rand(-0.5..0.5) }
end
that will spit back an array like this:
[-0.034680737617880486, 0.34802029078157803, 0.1346483808607455, 0.12155616615186282, -0.41043213731234474]

Ruby: how to make an array of words, with the length of the array being random [closed]

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 8 years ago.
Improve this question
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
end

Linq to read text file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a text file with contents as:
...
*..
*..
...
The vehicle used are:
*Car(Marathi, Nissan, Toyota)..
*Bikes(Yamaha, Hero Honda)
*...
*...
*...so on..
The items used are...
*..
*..
Now I need to search for the key word "vehicle" and put the options Car(Marathi, Nissan, Toyota)..), Bikes(Yamaha, hero honda), etc into a list.
i.e. Everything that comes after "*" in a line, must be an item of that list.
Linq must be used or any other means where loops are not allowed.
The desired result is not really clear.
If you need a List<string>, containing
"Car(Marathi, Nissan, Toyota).."
"Bikes(Yamaha, Hero Honda)"
"..."
"..."
"...so on..
you could do
var result = File.ReadAllLines(#"<pathToYourFile>")
//skip lines without "vehicle"
.SkipWhile(m => !m.Contains("vehicle"))
//skip the line with "vehicle"
.Skip(1)
//take the following lines starting with an "*"
.TakeWhile(m => m.StartsWith("*"))
//remove the "*"
.Select(m => m.Replace("*", string.Empty))
//enumerate to get a List<string>
.ToList();

Resources