This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Python Question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"""
Question 3

A dictionary is given to you where the value corresponding
to each key is the key to another value. For example:
dict = {1:2, 2:3, 3:4, 4:5, 5:-1}
In dict, the value of key 1 is 2, which is the key in 2:3.
Key 2, has value 3, which is the key in 3:4. One of the keys
has value -1, that is the last key in the chain and it doesn't
really point to another key-value pair. So in the above example
key 5 is the last item and it does not point to another key-value
pair.

Given such a dictionary, write a function that finds the first
item in the chain. The function must return the (key,value)
tuple for the first key in the chain. For the above example,
your function must return (1,2), because no other key has value 1.
"""

def firstKey(dict):





"""
Question 4

A dictionary is given to you as described in Question 3.
Write a function that converts this dictionary to a list,
such that for every pair of consecutive items in the list,
there is a corresponding key-value pair in the dictionary.
For example, given the following dictionary:
{5:1, 2:5, 1:3, 7:-1, 3:7}
your function must return:
[2, 5, 1, 3, 7]
So, 2:5, 5:1, 1:3, 3:7 and 7:-1 are key-value pairs in
the dictionary.
"""

def dictToList(dict):
 
Marshal
Posts: 69005
275
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us what logic you used to solve those questions. Then we shall see whether we think you have got it right.
Just as on all other boards, we don't use ALL UPPER‑CASE WRITING for normal text.
 
Akrep Kral
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried very hard but I couldn't do these 2 questions
 
Campbell Ritchie
Marshal
Posts: 69005
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what you tried, even if it was unsuccessful.
 
Saloon Keeper
Posts: 3918
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A hint:

what would happen if the very first key in the chain also appeared as a value somewhere in that Dictionary?
 
Akrep Kral
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please show us what you tried, even if it was unsuccessful.




def firstKey(dict):
   for k,v in dict.items():
       if v == -1:
           a = k
   while True:

i can come hear
 
Marshal
Posts: 15547
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Study this part of the question very carefully: "The function must return the (key,value) tuple for the first key in the chain. For the above example, your function must return (1,2), because no other key has value 1."

It gives you an idea of what you're looking for in order to determine which is the first key in the chain.
 
Campbell Ritchie
Marshal
Posts: 69005
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akrep Kral wrote:. . .
def firstKey(dict):
   for k,v in dict.items():
       if v == -1:
           a = k
   while True:
. . .

I worry when I see while true or similar. That creates an infinite loop.
Please use the code button, then your code woud look like this:-...and isn't that easier to read. Where does a come from? Why are you looking for −1? Look what Junilu said about the question. Can you work out which k doesn't appear as a v anywhere?
 
Akrep Kral
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Study this part of the question very carefully: "The function must return the (key,value) tuple for the first key in the chain. For the above example, your function must return (1,2), because no other key has value 1."

It gives you an idea of what you're looking for in order to determine which is the first key in the chain.




when we print this" print(firstKey({3:4, 5:-1, 1:2, 2:3, 4:5})) " , the answer needs to come to (5, -1) ?
 
Campbell Ritchie
Marshal
Posts: 69005
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the same dictionary as before? If it is, then (5, −1) is the last pair in the chain.
 
Junilu Lacar
Marshal
Posts: 15547
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akrep Kral wrote:when we print this" print(firstKey({3:4, 5:-1, 1:2, 2:3, 4:5})) " , the answer needs to come to (5, -1) ?


No. That would be the exact opposite of what is being asked.
 
Akrep Kral
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Akrep Kral wrote:when we print this" print(firstKey({3:4, 5:-1, 1:2, 2:3, 4:5})) " , the answer needs to come to (5, -1) ?


No. That would be the exact opposite of what is being asked.



where is the bug ???


 
Junilu Lacar
Marshal
Posts: 15547
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akrep Kral wrote:where is the bug ???


The bug is in your understanding of what needs to be done. Answer this question: How do you figure out which pair is the start of the chain?
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic