For example I have these 2 lists:
List A
Dog Tamal301*
Iguana Tamal345
Cat Tamal405
Snake Tamal408*
Cocodrile Tamal420
Bird Tamal467*
Parrot Tamal578*
List B
Tamal301* Orchid
Tamal320 Daisy
Tamal408* Poinsettia
Tamal467* Tulip
Tamal490 Rose
Tamal578* Chrysanthemums
(the * is just to emphazise that there are coincidences, it shouldn't be there)
I want to merge together list A and B, with only the matches.
Like this:
Dog Tamal301 Orchid
Snake Tamal408 Poinsettia
Bird Tamal467 Tulip
Parrot Tamal578 Chrysanthemums
I have a method to do it, but it is stupid, greps and for loops.
So I want to know if there is a better way to do it.
Thanks guys =D
Check the manual for the join command: man 1 join
Related
I'm making a simple command line game with Ruby and I'm having trouble saving some information without a database/HTTP dynamic.
For example, let's say I have to make a sandwich (in the game). I am presented with an array of ingredients to choose from, like so:
[1] Carrot
[2] Banana
[3] Cheese
[4] Tomato
I cannot hardcode a direct correspondence between number and ingredient because, before that, I was forbidden to use a couple of ingredients, at random (so the complete ingredients array is two items longer). And I don't want to present a list numbered like [1] [2] [4] [6] because it gets confusing.
What I'm doing right now is hardcoding a direct correspondence between a letter and an item, so for Banana press B, for Cheese press C and so on. But it's less than ideal, particularly because this is a pattern used throughout the game, and in some contexts it will get very inconvenient, both for me and the player.
So, is there a better way for me to do this? How can I associate an input with an item of a list that is generated randomly, and also save that information for further use down the line)?
Here's how I solved it:
Mario Zannone's comment made me realize I could use the index of the array elements as an id, whereas I had been looking at the whole thing as if it was sort of just text.
So here's the code I came up with to take advantage of that:
(0...#ingredients.length).each do |i|
puts "[#{i+1}] #{#ingredients[i]}"
end
That way I now have a direct correspondence between element and input with:
choice = gets.chomp.to_i - 1
#selected_ingredient = #ingredients[choice]
So I use sort to sort it via my first columm in vim.
apple bear
apple zoo
apple bar
banana hockey
banana football
But then, I want it to sort it on the second column, that it becomes this:
apple bar
apple bear
apple zoo
banana football
banana hockey
Any ideas how I can achieve this in vim?
First move to the start of the file:
gg
Then use 'sort -k 2' to sort the buffer:
!Gsort -k 2<ENTER>
The following worked for me, using Vim's built in sort function:
:sort! r/ /|sort
This works even if not sorted to begin with.
I'm tinkering with Prolog and ran into the following problem.
Assume I want to create a little knowledge base about the courses of an university.
I need the following two relation schemes:
relation scheme for lecturer: lecturer(Name,Surname)
relation scheme for course: course(Topic,Lecturer,Date,Location).
I have a lecturer, John Doe:
lecturer(doe,john).
John Doe teaches the complexity class:
course(complexity,lecturer(doe,john),monday,roomA).
Now I have a redundancy in the information - not good!
Is there any way to achieve something like this:
l1 = lecturer(doe,john).
course(complexity,l1,monday,roomA).
Many thanks in advance!
The same normalization possibilities as in data bases apply:
id_firstname_surname(1, john, doe).
and:
course_day_room_lecturer(complexity, monday 'A', 1).
That is, we have introduced a unique ID for each lecturer, and use that to refer to the person.
So, I'm going through a long list with different types of things. Let's say that it has names of different kinds of foods. The list might look something like this:
olive
potato
strawberry
potato
potato
strawberry
I want to store each object type and the number of times that object type occurs. Moreover, I cannot enumerate all of the object types in advance. I don't know what all of the foods will be beforehand.
I want to have something like this as the output:
potato (3)
strawberry (2)
olive (1)
Basically, a list of the object types in order of their frequency. What's the best data structure for this? Are there any built-in classes in Java that I could use that would prevent me from having to reinvent the wheel?
You can use HashMap<K,V>
Map<String,int> map = new HashMap<String,int>();
I would use a dictionary-like structure. Then basically your algorithm would look like this:
-Begin Loop
If current element not a key in dictionary:
dictionary(element) -> 0 (Dictionary at key 'element' refers to 0)
Else:
dictionary(element)++ (increment dictionary at key)
Then you could later loop through the keys and find their frequencies.
Michael G.
TL;DR: Need help calling a rule with a fact
I´ve started out with Prolog, coming from C and got stuff working... until they evidently got broken. I´m writing a small car-paint program for myself as I´m learning this new language
I'm trying to call a rule with a fact (is this possible?), what I want to do is use one fact "cars" and another fact "paint" to make one big list consisting of all the cars in all the different paints. I'm having trouble making the code work as I want...have a look
I´ve got the facts:
cars([ferrari, bmw, ford, jaguar]).
paints([red, white, blue, yellow]).
/*Now I wanted to loop through each car, eachtime printing
out the different paint combinations of that car: */
start:- loop_cars(cars(C)). /*starts loop_cars with all the cars e.g [ferrari...]*/
/*but it false here, even if C = [ferrari...]*/
loop_cars([]).
loop_cars([Ca|Rest]):-
loop_paints(Ca,paints(P)), /*The car is sent off for a paint job,...*/
loop_cars(Rest). /*...(cont from above) same false here as before*/
loop_paints(_,[]).
loop_paints(Ca,[Pa|Rest]):- /*This works*/
write([Ca,Pa]), /*Writes it like "[ferrari, white] [ferrari, blue] ..."*/
loop_paints(Ca,Rest).
So I guess I need help solving two problems:
How do i pass the contents of the facts cars and paints to the two loops?
A "garage" to put all the combinations in. Garage being a big list consisting of small 2-items-lists (the car and paint).
You can do it like this:
start :- cars(C), loop_cars(C).
First, “assign” (I think it's called “unify” in Prolog terminology) the list of cars to the variable C and then call loop_cars for this list. Similarly with paints.
If you want to store the result in a variable, you have to add an “output” parametr to your predicates:
loop_paints(_,[],[]).
loop_paints(Ca,[Pa|Rest],[Res|ResRest]):-
Res = [Ca,Pa],
loop_paints(Ca,Rest,ResRest).