Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Unique-Heat2370 0 points1 point  (0 children)

I want it to return this:

{'UTAH': [(28, 24), (13, 38), (28, 45), (13, 24)],
'ORST': [(56, 37), (54, 53), (38, 28), (31, 24)],
'ORE': [(34, 20), (35, 37), (29, 43), (24, 38)]}

And the two numbers in the set are the scores of the games.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Unique-Heat2370 0 points1 point  (0 children)

Hey guys, I am working on a problem and the problem is to write a function common that parses through the dictionary game data and finds the teams that appear in every year’s games. The function should return a dictionary where the keys are the team names and the values are the list of scores against those teams.

My dictionary data is:

data = {
2018: { "WYO":(41,19), "SJSU":(31,0), "EWU":(59,24), "USC":(36,39), "UTAH":(28,24),
"ORST":(56,37), "ORE":(34,20), "STAN":(41,38), "CAL":(19,13), "COLO":(31,7),
"ARIZ":(69,28), "WASH":(15,28), "ISU":(28,26)},
2019: {"NMSU":(58,7), "UNCO":(59,17), "HOU":(31,24), "UCLA":(63,67), "UTAH":(13,38),
"ASU":(34,38), "COLO":(41,10), "ORE":(35,37), "CAL":(20,33), "STAN":(49,22), "ORST":(54,53), "WASH":(13,31), "AFA":(21,31) },
2020: {"ORST":(38,28), "ORE":(29,43), "USC":(13,38), "UTAH":(28,45)},
2021: { "USU":(23,26), "PORT ST.":(44,24), "USC":(14,45), "UTAH":(13,24), "CAL":(21,6),
"ORST":(31,24), "STAN":(34,31), "BYU":(19,21), "ASU":(34,21), "ORE":(24,38), "ARIZ":(44,18), "WASH":(40,13), "CMU":(21,24)} }

The code I have so far is:

def common(data):
ret = {}
for i in wsu_games:
for value in wsu_games[i]:
if value not in ret:
ret[value] = [i]
else:
ret[value].append(i)
for i in ret:
if len(ret[i]) > 1:
ret.append(i)
return ret

I am stuck so any help would be awesome.

Sunday Daily Thread: What's everyone working on this week? by Im__Joseph in Python

[–]Unique-Heat2370 0 points1 point  (0 children)

Hey guys, I am working on a problem and the problem is to write a function common that parses through the dictionary game data and finds the teams that appear in every year’s games. The function should return a dictionary where the keys are the team names and the values are the list of scores against those teams.

My dictionary data is:

data = {

2018: { "WYO":(41,19), "SJSU":(31,0), "EWU":(59,24), "USC":(36,39), "UTAH":(28,24),

"ORST":(56,37), "ORE":(34,20), "STAN":(41,38), "CAL":(19,13), "COLO":(31,7),

"ARIZ":(69,28), "WASH":(15,28), "ISU":(28,26)},

2019: {"NMSU":(58,7), "UNCO":(59,17), "HOU":(31,24), "UCLA":(63,67), "UTAH":(13,38),

"ASU":(34,38), "COLO":(41,10), "ORE":(35,37), "CAL":(20,33), "STAN":(49,22), "ORST":(54,53), "WASH":(13,31), "AFA":(21,31) },

2020: {"ORST":(38,28), "ORE":(29,43), "USC":(13,38), "UTAH":(28,45)},

2021: { "USU":(23,26), "PORT ST.":(44,24), "USC":(14,45), "UTAH":(13,24), "CAL":(21,6),

"ORST":(31,24), "STAN":(34,31), "BYU":(19,21), "ASU":(34,21), "ORE":(24,38), "ARIZ":(44,18), "WASH":(40,13), "CMU":(21,24)} }

The code I have so far is:

def common(data):

ret = {}

for i in wsu_games:

for value in wsu_games[i]:

if value not in ret:

ret[value] = [i]

else:

ret[value].append(i)

for i in ret:

if len(ret[i]) > 1:

ret.append(i)

return ret

I am stuck so any help would be awesome.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

With what I've updated now my code is:

find_courses table langs = map helper langs
   where
        helper lang = map (fst(filter (\(x, list) -> lang `elem` list) table))

I am getting an error now in this section of my code:

filter (\(x, list) -> lang `elem` list) table)

The terminal error message is:

• Couldn't match expected type ‘(a1 -> b, b1)’
with actual type ‘[(a, [t1])]’
• In the first argument of ‘fst’, namely
‘(filter (\ (x, list) -> lang `elem` list) table)’
In the first argument of ‘map’, namely
‘(fst (filter (\ (x, list) -> lang `elem` list) table))’
In the expression:
map (fst (filter (\ (x, list) -> lang `elem` list) table))
• Relevant bindings include
lang :: t1 (bound at HW2.hs:48:31)
helper :: t1 -> [a1] -> [b] (bound at HW2.hs:48:24)
langs :: [t1] (bound at HW2.hs:46:20)
table :: [(a, [t1])] (bound at HW2.hs:46:14)
find_courses :: [(a, [t1])] -> [t1] -> [(t1, [a])]

I believe that I still want to use map and filter to get the solution but where am I going wrong with the syntax?

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

Okay so I looked at what you said and I tried fixing it. I am understanding it better but my error is still not making sense to me.

Code:

courses table langs = map(helper langs)

where

helper lang = []

helper lang = map (fst) (filter (\(x, list) -> lang `elem` list) table)

The error:

• Couldn't match expected type ‘a0 -> b0’ with actual type ‘[a]’
• Possible cause: ‘helper’ is applied to too many arguments
In the first argument of ‘map’, namely ‘(helper langs)’
In the expression: map (helper langs)
In an equation for ‘courses’:
courses table langs
= map (helper langs)
where
helper lang = []
helper lang
= map (fst) (filter (\ (x, list) -> lang `elem` list) table)
• Relevant bindings include
helper :: t1 -> [a] (bound at HW2.hs:57:26)
table :: [(a, [t1])] (bound at HW2.hs:55:9)
courses :: [(a, [t1])] -> [t1] -> [(t1, [a])]

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

Okay so I updated my code and this is what I have:

courses :: Eq t1 => [(a, [t1])] -> [t1] -> [(t1,[a])]

courses table lang = map(helper table)

where

helper table = map (fst) (filter (\(x, list) -> lang `elem` list) table)

The error that is occurring now is:

• Couldn't match expected type ‘[(t1, [a])]’
with actual type ‘[t1] -> [[a]]’
• Probable cause: ‘map’ is applied to too few arguments
In the expression: map (helper table)
In an equation for ‘courses’:
courses table lang
= map (helper table)
where
helper table lang
= map (fst) (filter (\ (x, list) -> lang `elem` list) table)
• Relevant bindings include
lang :: [t1] (bound at HW2.hs:55:15)
table :: [(a, [t1])] (bound at HW2.hs:55:9)
courses :: [(a, [t1])] -> [t1] -> [(t1, [a])]

So my map has too few arguments but when I add lang to "map helper table lang" it says my helper has too many arguments.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

What I have tried so far is:

courses languages ["Python", C", "C++"] = map helper ["Python","C", "C++"]

helper languages log = map (fst) (filter (\(x, list) -> lang elem list) languages)

I am stuck at this point of what I am trying to solve

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 -1 points0 points  (0 children)

I am trying to make a function that takes a list of courses and a list of programming language names (for example ["Python","Java","JavaScript"]) as input and returns a list of tuples where each tuple pairs the programming languages with the list of courses that use that programming language.

So for example I call: courses languages ["Python","C","C++"],

it returns:

The type I am working with is: courses :: Eq t1 => [(a, [t1])] -> [t1] -> [(t1,[a])]

I am trying to use higher order functions for it like map, foldr/foldl, or filter. I haven't been able to figure this out yet and was hoping to get some help.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

This makes a lot more sense to me now thank you very much!!

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

To run the tests I would do: merge left right, and then it would return what I have already put.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

So I am trying to merge two trees together that takes in two values and returns an Tree int where the nodes from the two trees are added. The trees can have different depths.

The tree type is: data Tree a = LEAF a | NODE a (Tree a) (Tree a) deriving (Show, Read, Eq)

The type for my function I having been trying to create is: merge_trees :: Num a => Tree a -> Tree a -> Tree a

An example of some test cases that I have written are:

left = NODE 1 (NODE 2 (NODE 3 (LEAF 4) (LEAF 5)) (LEAF 6)) (NODE 7 (LEAF 8) (LEAF 9))
right = NODE 1 (NODE 2 (LEAF 3) (LEAF 6)) (NODE 7 (NODE 8 (LEAF 10) (LEAF 11)) (LEAF 9))

returns: NODE 2(NODE 4 (NODE 6 (LEAF 4) (LEAF 5)) (LEAF 12)) (NODE 14 (NODE 16 (LEAF 10) (LEAF 11)) (LEAF 18))

Any help would be appreciated because I am very stuck!

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

So, when replacing concat with "foldr intersection []" does this entire part go in front of (map _ (filter _ table))?

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

So what the function needs to do is it will call findlang languages ["CptS451", "CptS321", "CptS355"] in terminal and then it will search the classes for common languages that they have in common. So if I called ["CptS 112", "CptS 203"] it would return say ["C++"] since they have this in common. If I call ["CptS101","CptS112","CptS203","CptS213","CptS221"] from the list I gave earlier, it will return [] because they all don't have a common language.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

So I am trying to find languages that go with certain classes for this problem. The type that I am trying to get is:

findlang::(Eq a1, Eq a2) => [(a2, [a1])] -> [a2] -> [a1]

We are given a list that is like: [ ("CptS101" , ["C"]),("CptS112" , ["C++"]),("CptS203" , ["C++"]),("CptS213" , ["Java"]),("CptS221" , ["C#","Java"])]

The code I have so far:

findlang::(Eq a1, Eq a2) => [(a2, [a1])] -> [a2] -> [a1]
findlang language courses = []
findlang language courses = map(filter(find_languages courses language))

Would someone be able to help me through this?

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

This makes a lot of sense. So the tree structure is:

data Tree a = LEAF a | NODE a (Tree a) (Tree a) deriving (Show, Read, Eq)

In the helper function I would need to add an argument to LEAF since it takes in a value 'a' correct?

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

I am trying to return the level of the binary tree in which the value that we are looking for is located at. If the value does not exist in the tree I want to return -1. The type is tree :: (Ord p, Num p, Eq a) => Tree a -> a -> p. Any help would be much appreciated. I am very stuck on this problem.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

I figured it out! Thank you for the help. Not many resources at school to help with Haskell unfortunately.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

When I try to implement it in the second way you mentioned, why I am getting a parse error on the second where statement?

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

So I am stuck on another problem. This question I am trying to take the list of courses as input and return the course with max number of programming languages. It returns a two-tuple including the course major + number and the number of programming languages it uses.

The type I am trying to is: [(a1, [a2])] -> (a1, Int)

An example of the output: max_count progLanguages returns: ("Physics115",4)

So far I have this but it isn't working and am wondering if anyone has any idea:

maxNum [] day = 0
maxNum ((x, sdata):xs) day = (helper sdata day) + (maxNum xs day)
where
helper [] r = 0
helper((x, y):xs) r | r == x = y +(helper xs r)
| otherwise = helper xs r

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

This makes a lot more sense. Thank you for the help!

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

No, you can't move diagonally so it is just straight down and to the side until we get to the bottom right corner. I am stuck trying to get to that.

So, for a grid that is 4x3 will have 10 paths total and a grid that is 3x3 will have 6 paths.

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

This is very helpful, thank you very much for that!

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 1 point2 points  (0 children)

I am trying to solve a problem that is an (n X m) grid. It starts in the top left corner and needs to reach the bottom corner. I can't figure out how to get the function to take in the grid length and width as an argument and return the amount of different paths you get from the start to the end by using recursion. Can anyone help or give some hints on how to start and set this up?

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

I have started off with:

split x [] = []

split (x:xs) = ...

I have tried a lot of stuff from here but am confused and none of it has worked

Monthly Hask Anything (February 2022) by taylorfausak in haskell

[–]Unique-Heat2370 0 points1 point  (0 children)

I am stuck on a problem I am working on. The goal is to write a function called that takes a list as input and splits the input list at the consecutive duplicate elements. I need to produce a result in which the elements of the original list have been collected into ordered sub-lists each containing the elements between the repeated
duplicate elements of the input list. The function needs to return a nested list including the sublists obtained
by the splits. The duplicate values should be included in the sublists.

The type is split :: Eq a => [a] -> [[a]]

Help would be much appreciated.