Adam is thinner than Barbara. Dalton is less thin than Celia. Adam is less thin than Celia - theory

Option:
A) Adam is thinner than Dalton.
B) Bárbara is thinner than Adam.
C) Célia is thinner than Bárbara.
D) Célia is less thin than Dalton.
E) Bárbara is thinner than Dalton.
The right response is C, but not function in the following code:
adam='a'
barbara='b'
dalton='c'
celia='d'
adam = dalton
barbara > adam
barbara > celia
celia < dalton
barbara > dalton
adam > celia
Because when I check the option B, D and E is True and C is False.

When I replace the string for integer the code work properly.
adam = 2
barbara = 3
dalton = 2
celia = 1
adam = dalton
barbara > adam
barbara > celia
celia < dalton
barbara > dalton
adam > celia
Now the C is True and others options is False.

Related

How to use AWK to print associative array in a loop correctly?

Beth 45 0
Danny 33 0
Thomas 22 40
Mark 65 100
Mary 29 121
Susie 39 76.5
Joey 51 189.52
Peter 23 78.26
Maximus 34 289.71
Rebecca 21 45.79
Sophie 26 28.44
Barbara 24 107.36
Elizabeth 35 105.69
Peach 40 102.69
Lily 41 123
The above is a data file which has three fields: name, age, salary.
I want to print average salary, number, and names for people aged above 30 and under 30.
In this exercise, I want to practise using strings as subscripts.
Here is my AWK code:
BEGIN { OFS = "\t\t" }
{
if ($2 < 30)
{
a = "age below 30";
salary[a] += $NF;
count[a]++;
name[a] = name[a] $1 "\t";
}
else
{
a = "age equals or above 30";
salary[a] += $NF;
count[a]++;
name[a] = name[a] $1 "\t";
}
}
END {
for (a in salary)
for (a in count)
for (a in name)
{
print "The average salary of " a " is " salary[a] / count[a];
print "There are " count[a] " people " a ;
print "Their names are " name[a];
print "********************************************************";
}
}
The following is the output:
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The output is very difficult for me to understand.
What I anticipated should look like this:
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The average salary of age equals or above 30 is 109.679
There are 6 people age below 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age equals or above 30 is 109.679
There are 6 people age below 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The average salary of age below 30 is 70.1417
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age below 30 is 70.1417
There are 9 people age equals or above 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age below 30 is 70.1417
There are 6 people age below 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
So my first question is : Where did I understand wrong?
And my second question is :
I actually don't need so many loops. I just need
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Thomas Mary Peter Rebecca Barbara Sophie
********************************************************
for (a in salary, count, names) doesn't work. Is there a better way ?
for (x in salary)
for (y in count)
for (z in name)
print "foo"
says for every index in salary, loop through every index in count and while doing so, for every index in count loop through every index in name and print "foo" each time. So if salary, count, and name each had 3 entries then you'd print "foo" 3*3*3 = 9 times.
It gets more complicated than that in your code though because you're using the same variable to hold the index value of each array at every level of the nested loop:
for (a in salary)
for (a in count)
for (a in name)
so I'm not sure what awk is going to do with that - it may even be undefined behavior.
Since all 3 arrays have the same indices, just pick one of the arrays and loop on it's indices and then you can access all 3 arrays using that same index.
$ cat tst.awk
{
bracket = "age " ($2 < 30 ? "under" : "equals or above") " 30"
names[bracket] = (bracket in names ? names[bracket] "\t" : "") $1
count[bracket]++
salary[bracket] += $NF
}
END {
for (bracket in names) {
print "The average salary of", bracket, "is", salary[bracket] / count[bracket]
print "There are", count[bracket], "people", bracket
print "Their names are", names[bracket]
print "********************************************************"
}
}
$ awk -f tst.awk file
The average salary of age equals or above 30 is 109.679
There are 9 people age equals or above 30
Their names are Beth Danny Mark Susie Joey Maximus Elizabeth Peach Lily
********************************************************
The average salary of age under 30 is 70.1417
There are 6 people age under 30
Their names are Thomas Mary Peter Rebecca Sophie Barbara
********************************************************

Sapply with two conditions

I have two tables. Which have the kind of formatting shown below. One of it is table A as such:
students|Test Score|Year
A | 100 |1993
B | 81 |1992
C | 92 |1992
D | 88 |1993
Another table B I have looks like this:
Class | Students | Year
1 | {A,D} |1993
2 | {B,C} |1992
I would like to perform some sort of manipulation in R where by I can search for the students listed in the array under the column in table B from table A and tabulate the scores into the following format:
Class | Students | Mean Score
1 | {A,D} | 94
2 | {B,C} | 86.5
Is there any formula which I can use to do the searching and then merge those results by some manipulation in R?
I know the above can be done with:
B$MeanScore <- sapply(strsplit(gsub("[{}]","", B$Students), split=","),
function(x) mean(A$Test.Score[A$Students %in% x]))
But is there a way for me to add a second condition which is to match the year as well. The year of the class as well as the year of the test.
In full agreement with jogo here:
A <- data.frame(students = c("A","B","C","D"), `Test Score` = c(100,81,92,88), Year = c(1993,1992,1992,1993))
A
# students Test.Score Year
#1 A 100 1993
#2 B 81 1992
#3 C 92 1992
#4 D 88 1993
B <- data.frame(Class = c(1,2), Students = c("{A,D}","{B,C}"), Year = c(1993,1992))
B
# Class Students Year
#1 1 {A,D} 1993
#2 2 {B,C} 1992
colnames(A) # taking note of the case sensitive "students" and "Year"
#[1] "students" "Test.Score" "Year"
s <- strsplit(gsub("[{}]","",B$Students), ",")
B.long <- data.frame(students = unlist(s),
Class = rep(B$Class, sapply(s, length)),
Year = rep(B$Year, sapply(s, length)))
B.long
#Students Class Year
#1 A 1 1993
#2 D 1 1993
#3 B 2 1992
#4 C 2 1992
Newdf <- merge.data.frame(A, B.long, c("Year","students"))
#Year students Test.Score Class
#1 1992 B 81 2
#2 1992 C 92 2
#3 1993 A 100 1
#4 1993 D 88 1
aggregate(Test.Score ~ Year + Class, Newdf, mean)
#Year Class Test.Score
#1 1993 1 94.0
#2 1992 2 86.5

Sorting Awk array by last name

In my script, I start with a file of campaign contributors and anyone who donates a collective $500 is eligible for a contest. Anyone who meets that criteria I add to an array with an incrementing index to adjust the size as needed. Each index is formatted as outlined below, with the X's being a phone number. In the END portion of the script, I need to sort this array by last name($2) for printing. I've done some searching but come up empty handed. I'm not asking for someone to type the script for me, merely to point me in a better direction of search or offer advice. I need help sorting the array contestants as currently it will be filled properly with the string values the way I need them for the assignment.
Where v1,2, & 3 are the campaign contributions, I am using -F'[ :]' in my command to get both spaces and colons as field separators.
Input File lab4.data
Fname Lname:Phone__Number:v1:v2:v3
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300
Array to hold anyone > $500, $8 is created and holds the value $5+$6+$7:
the array is initialized and filled in for loop given below
$8 = $5+$6+$7;
contestants[len++]
Loop to check add people to contestant array.
name and number are arrays that hold their respective values for later use.
for(i=0;i<=NR;i++)if(contrib[i]>500){contestants[len++]= name[i]" "number[i] }
Formatting of indexes(desired array values for contestant[len++]):
[0] Mike Harrington (510) 548-1278
[1] Archie McNichol (206) 548-1348
[2] Guy Quigley (916) 343-6410
[3] Dan Savage (406) 298-7744
[4] John Goldenrod (916) 348-4278
[5] Tom Savage (408) 926-3456
[6] Elizabeth Stachelin (916) 440-1763
Loop to print/check that array has been correctly filled(it is)
for (i=0; i <len; i++) {print contestants[i]}
Output:
Mike Harrington (510) 548-1278
Archie McNichol (206) 548-1348
Guy Quigley (916) 343-6410
Dan Savage (406) 298-7744
John Goldenrod (916) 348-4278
Tom Savage (408) 926-3456
Elizabeth Stachelin (916) 440-1763
Desired Final Output: Ignore formatting as it correctly displays in my terminal I just hard a hard time getting it all nice in here.
***FIRST QUARTERLY REPORT***
***CAMPAIGN 2004 CONTRIBUTIONS***
Name Phone Jan | Feb | Mar | Total Donated
Mike Harrington (510)548-1278 $ 250 $ 100 $ 175 $ 525
Christian Dobbins (408)538-2358 $ 155 $ 90 $ 201 $ 446
Susan Dalsass (206)654-6279 $ 250 $ 60 $ 50 $ 360
Archie McNichol (206)548-1348 $ 250 $ 100 $ 175 $ 525
Jody Savage (206)548-1278 $ 15 $ 188 $ 150 $ 353
Guy Quigley (916)343-6410 $ 250 $ 100 $ 175 $ 525
Dan Savage (406)298-7744 $ 450 $ 300 $ 275 $ 1025
Nancy McNeil (206)548-1278 $ 250 $ 80 $ 75 $ 405
John Goldenrod (916)348-4278 $ 250 $ 100 $ 175 $ 525
Chet Main (510)548-5258 $ 50 $ 95 $ 135 $ 280
Tom Savage (408)926-3456 $ 250 $ 168 $ 200 $ 618
Elizabeth Stachelin (916)440-1763 $ 175 $ 75 $ 300 $ 550
-----------------------------------------------------------------------------
SUMMARY
-----------------------------------------------------------------------------
The campaign received a total of $6137.00 for this quarter.
The average donation for the 12 contributors was $511.42.
The highest total contribution was $1025.00 made by Dan Savage.
***Thank you Dan Savage***
The following people donated over $500 to the campaign.
They are eligible for the quarterly drawing!!
Listed are their names(sorted by last names) and phone numbers.
John Goldenrod (916) 348-4278
Mike Harrington (510) 548-1278
Archie McNichol (206) 548-1348
Guy Quigley (916) 343-6410
Dan Savage (406) 298-7744
Tom Savage (408) 926-3456
Elizabeth Stachelin (916) 440-1763
Thank you all for your continued support!!
Using gawk, this is straightforward to do with the in-built sort functions, e.g.
BEGIN {
data["Jane Doe (123) 456-7890"] = 600;
data["Fred Adams (123) 456-7891"] = 800;
data["John Smith (123) 456-7892"] = 900;
exit;
}
END {
for (i in data) {
split(i,x," ")
data1[x[2] " " x[1] " " x[3] " " x[4]] = i;
}
asorti(data1,sdata1);
for (i in sdata1) {
print data1[sdata1[i]],"\t",data[data1[sdata1[i]]];
}
}
... which produces:
Fred Adams (123) 456-7891 800
Jane Doe (123) 456-7890 600
John Smith (123) 456-7892 900
In plain awk, the same result can be achieved by writing the array indices to a file, sorting that file and then reading the file back using getline.
The way to approach this is to produce the pre-SUMMARY output as you read the data so you don't need to store all of your data in an array, just the people who contributed more than $500 and just insert them into the array in the desired order using an insertion sort algorithm.
You would do it something like this:
awk -F':' '
NR==1 {
print "header stuff"
next
}
{
tot = $3 + $4 + $5
printf "%-20s%10s $%5s $%5s $%5s $%5s\n", $1, $2, $3, $4, $5, tot
}
tot > 500 {
split($1,name,/ /)
surname = name[2]
numContribs++
# insertion sort, check the algorithm:
for (i=1; i<=numContribs; i++) {
if (surname > surnames[i]) {
for (j=numContribs; j>i; j--) {
surnames[j+1] = surnames[j]
contribs[j+1] = contribs[j]
}
surnames[i] = surname
contribs[i] = $1 " " $2
break
}
}
}
END {
print "SUMMARY and text below it and then the list of $500+ contributors:"
for (i=1; i<=numContribs; i++) {
print contribs[i]
}
}
' lab4.data
The above is not a fully functional program. It's just intended to show you the right approach per your request.

how to sort a database

I have a database that i now combined using this function
def ReadAndMerge():
library1=input("Enter 1st filename to read and merge:")
with open(library1, 'r') as library1names:
library1contents = library1names.read()
library2=input("Enter 2nd filename to read and merge:")
with open(library2, 'r') as library2names:
library2contents = library2names.read()
print(library1contents)
print(library2contents)
combined_contents = library1contents + library2contents # concatenate text
print(combined_contents)
return(combined_contents)
The two databases originally looked like this
Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
and this
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2
After being combined they now look like this
Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2
if i wanted to sort this database by last names how would i go about doing that?
is there a sort function built in to python like lists? is this considered a list?
or would i have to use another function that locates the last name then orders them alphabetically
You sort with the sorted() method. But you can't sort just a big string, you need to have the data in a list or something similar. Something like this (untested):
def get_library_names(): # Better name of function
library1 = input("Enter 1st filename to read and merge:")
with open(library1, 'r') as library1names:
library1contents = library1names.readlines()
library2=input("Enter 2nd filename to read and merge:")
with open(library2, 'r') as library2names:
library2contents = library2names.readlines()
print(library1contents)
print(library2contents)
combined_contents = sorted(library1contents + library2contents)
print(combined_contents)
return(combined_contents)

combinations of players for a team in C

I am trying to generate all combinations of players to make up a team of basketball players.
Let's say there's 5 positions(SG, PG, SF, PF, C) and I need to fill a rooster with 9 players, 2 of each position except the Center position for which there's only 1.
Let's say I have 10 players for each position, how can I generate a list of all possible permutations.
I would like to import the names from excel in a csv file, and then output all the combinations back to excel in another csv file.
I can figure out how to do the importing and exporting csv stuff, but i am more interested in the best algorithm to do the above permutations.
If it is easier to generate permutations, that is fine as well as I can easily eliminate duplicates in excel.
Thanks!
You could use an algorithmic technique called backtracking.
Or, depending on how many players you have, you could use brute force and just loop. For example, you could use the following to select all the combinations of 2 forwards and 1 center (this is a C++ sample just shown to illustrate the technique).
#include <iostream>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector< string > centers;
vector< string > forwards;
centers.push_back("joey");
centers.push_back("rick");
centers.push_back("sam");
forwards.push_back("steve");
forwards.push_back("joe");
forwards.push_back("harry");
forwards.push_back("william");
for(int i = 0; i < centers.size(); ++i) {
for(int j = 0; j < forwards.size(); ++j) {
for(int k = j+1; k < forwards.size(); ++k) {
printf("%s %s %s\n",centers[i].c_str(), forwards[j].c_str(), forwards[k].c_str());
}
}
}
return 0;
}
Output:
---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
joey steve joe
joey steve harry
joey steve william
joey joe harry
joey joe william
joey harry william
rick steve joe
rick steve harry
rick steve william
rick joe harry
rick joe william
rick harry william
sam steve joe
sam steve harry
sam steve william
sam joe harry
sam joe william
sam harry william
> Terminated with exit code 0.
However, it's important to remember that if you have a lot of players, anything you do that's "brute force", which would include backtracking (backtracking is the same idea as the loops I used above, only it uses recursion) is going to grow exponentially in running time. So for example for a 5 man roster, if you have 10 centers, 20 forwards, and 18 guards, then the running time is basically:
10 * 20 * 20 * 18 * 18 = 1,296,000
(20 * 20 because we need 2 fowards, and 18 * 18 because we need 2 guards).
1,296,000 is not too bad for running time, but when you start talking about 9 man rosters, you get much higher running times, because now you're dealing with alot more combinations.
So it depends on how much data you have as to whether this is even feasible.

Resources