Linq to read text file [closed] - linq

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();

Related

(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.

Matching partial strings in a phrase [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 put up an intranet site that loops through a .csv dump of our customer database and uses a form to help look up account numbers.
I want to treat all of my keywords as wild card terms, but respect their order. For example, if I have company A: "The Monogramme Shoppe" and company B: "Monograms & More at The Shop", I want to return A and B options if I type "mono shop" in the form field. This code does that:
company_lookup = company_lookup.split(" ")
counter = company_lookup.length
company_lookup.each do |com|
if company.downcase.include? com.downcase
counter = counter - 1
end
end
if counter == 0
match_found = true
account_number = row[2].to_s
matches.push [account_number, company]
end
But if I type "mono the", I also get both results. There, I only want to get the B result.
Is there any way to use regular expressions to, say look for PartialKeyword1 and PartialKeyword2 in a string and return true if matched?
You can use the following code to construct a regular expression to match the company name, and then use this regular expression to find the matched record.
company_lookup = company_lookup.split(" ").map{|r| Regexp.quote(r)}.join('.*?')
if company =~ /#{company_lookup}/i
matches.push [row[2].to_s, company]
end
If performance is a big concern, or the data size is huge, you'd better try some full text search engine, such as Thinking Sphinx

Interpreting "takes as input a sorted list of N integers output will be sorted squared integer list" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
how would you interpret that sentence in order to start coding. do you take it as the user inputs the number into an array or does it come from a text document for example and just scan it.
I already did it for both but I only have one submission so I have to make it count
I think the requirement is very clear - you need to write a function that receives a sorted list of integers, and returns a list of their squares.
E.g., in Java:
public List<Integer> squareList (List<Integer list) {
List<Integer> retVal = new ArrayList<>(list.size());
for (int item : list) {
retVal.add(item * item);
}
Collections.sort(retVal);
return retVal;
}

Add element to array and generate Hash with two arrays in ruby [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 9 years ago.
Improve this question
Need to add two arrays before that need to add some value at starting of the first array. Look at the following:
#conunty_format = [ "country", "imps", "revenue","network_revenue"]
final_ca = [2000,55.62,88.69]
I need to add "Canada" to final_ca and generate hash with corresponding county_format.
Hash[#conunty_format.zip(final_ca.unshift('canada'))]
=> {"country"=>"canada", "imps"=>2000, "revenue"=>55.62, "network_revenue"=>88.69}
You can use Array Zip and some properties of Array to achieve it in a single line. see the below code.
resulted_hash = #country_format.zip(final_ca.unshift("Canada")).inject({}) do |r, s| r.merge!({s[0] => s[1]}) end

Sorting Lunch Items Stored as Hash Values [closed]

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

Resources