Pyhton ile ilgili ingilizce sorular

Kodla Büyü
Mesajlar
2
Merhaba arkadaşlar, Pyhton da ingilizce iki sorum var bilenler yazabilirse sevinirim.

soru 1:
Write a function that takes 2 dictionaries as arguments and merges them into a new dictionaryand returns this new dictionary.
If the same key exists in both dictionaries, then the values from each dictionary is combined as a tuple and stored as the value of the key in thenew dictionary. The value from the first dictionary comes first in the tuple.


def mergeDicts(dict1, dict2):


Soru2:
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 keyin 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-valuepair. So in the above example key 5 is the last item and it doesnot point to another key-value pair.

Given such a dictionary, write a function that finds the first item in the chain. The function mustreturn 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):
 
1.
def mergeDict(dict1,dict2):
for k,v in dict2.items():
try:
a = dict1[k]
dict1[k] = [a,v]
except:
dict1[k] = v
return dict1

2.
def firstKey(dict):
for k,v in dict.items():
if v == -1:
a = k
while True:
for k,v in dict.items():
if a == k:
return (k,v)
if v == a:
a = k
 
Çok teşekkür ediyorum hocam sağolun. Ben de yeni öğreniyorum bir sorum daha var bakabilirseniz :

Write a function that takes a dictionary, a key and a value and adds the key-value pair according to the following rules:

- if the key does not exist in the dictionary, then the key-value pair is added normally to the dictionary
- if the key already exists, andthere is only one corresponding value of it
- then if the new value is different than the existing value, a list of the existing value and the new value is created and stored as the value of the key
- if the new value is the same as the existing value, then nothing is done
- if the key already exists, and there is a list of corresponding values
- if the new value is not contained in this value list, then it is added to the list,
- otherwise, nothing is done.

The function returns the updated dictionary.
The order of values in value lists must match the order they were inserted.

def multiAdd(dict, key, value):
 
İlk soruların da cevabını yazmadan önce sormam gereken soruyu sorayim.

Problemleri çözmek için neler yaptınız, nerelerde takildiginiz. Bunları yazarsanız ona göre yardımcı olan, takildiginiz yerde yol gosterecek birileri çıkar.

Yoksa odevinizi başkasına yaptırmanız size bişey kazandırmaz. Ayrica, ingilizce soruların Türkçe çevirisini koymanız, size yardımcı olmak isteyen kişilere de saygınızı gösterir.
 
Geri
Üst