I would like to write a program count the items that appear in BOTH list
[code=python]
A=['A', 'B', 'C','D', 'F','G', 'H']
B=['FF', 'GG', 'A','B', 'DD','EE', 'GG']
for i in range(len(A)):
count=0
for k in range(len(B)):
if A[i]==B[k]:
count+=1
count
[/code]
the result is 0 but it should be 2 (for 'A' and 'B')...
User Profile
Collapse
-
recursing question
I have the code
[code=python]
def calculation(n):
print 'n =', n
if n<=1:
return n
if n%2==0:
print 'Even'
return calculation(n-1) + calculation(n-2) + 1
else:
print 'Odd'
return n
print calculation(4)
[/code]
the result is
[code=python]... -
Thanks for your explanation, now I understand why it prints out n = ...
but I still don't understand why the final line is 3 -
list question
I would like to write a function test(data) to test whether the list has two or more elements refer to the same underlying object
[code=python]
>>>a = range(2)
>>>b = range(4)
>>>c = range(2)
>>>test([a,b,c])
False
>>>test([a,b,c,b]) #b appears 2 times
True
[/code]
If it is a list of numbers or a list of strings like
[1,2,3,4]... -
explanation for recursion problem
I have a python code and I don't understand how it works.
[code=python]
def cal(n):
print 'n =', n
if n<=1:
return n
else:
return cal(n-1) + cal(n-2)
#when I run
>>cal(4)
n = 4 # it is because I use parameter 4
n = 3 # is it because of cal(n-1), n=4-1?
n = 2 # is it because of cal(n-2), n=4-2? the rest I can get
n = 1
n = 0
... -
Inheritance in python
I have a python code
[code=python]
class Name:
def __init__(self, name, age):
self.name = name
self.age = age
def Information(sel f):
print ' Name: ', self.name
print ' Age: ', self.age
class Identity:
def __init__(self, name, age, ssn):
self.name = name
... -
It is a little complicated and It seems that there is something I don't know. Like I never read something that when defining a method, we don't use self._something
assume that I have self._month, self._day, self._year has been property defined.
I have two list of maximum day for regular year and leap year.
I would like to write a nextDate() method to increase one day of current date, how can I write it?
...Leave a comment:
-
Thank you very much for helping me out.
Can I do this thing in the class?
[code=python]
#assume I've already had CalculationDate class, when I run this class
>> CalculationDate (14,32,2004)
invalid date, please enter a gain 12/29/2004
[/code]Leave a comment:
-
The way I explained the problem might no be clear.
- The date (including month, day, and year for example Tues August 27, 2007) should be inputed by user.
- The class should have some methods to figure out what is the text input what is the numeric input, and be be to do the nextDate() which returns the next day of the year, this has to deal with the leap year.
Question: how can I write this?Leave a comment:
-
I only need the date like 11-11-2007, not the time. And this date is set by user, not accorded to the system time because if using the system time, it automatically recognizes the leap year, and there is no nextDate method is usedLeave a comment:
-
implementing a calendar class
I would like to write a class CalculationDate which can deal with a date in numerical formation and text format and I have a nextDate method that returns the next day of the year. This class is written with a unit test which can test the proper treatment of leap year.
My question is how can I able to write a code to deal with the numerical formation and text format? I usually write a numerical one then write a function to convert... -
-
I tried to test individual implementation but both gave the same result. Can anyone give a case that two implementations will have two different results?Leave a comment:
-
Unit testing for two difference implementations
[code=python]
class Calculation:
def __init__(self, A=0, B=1):
factor = gcd( abs(A), abs(B) )
if B < 0:
factor = -factor
self._numA = A // factor
self._denoB = B // factor
def __str__(self):
return str(self._numA) + '/' + str(self._denoB )
# now I have 2 different implementations for __int__"... -
thank you very much. I've purchased a python book, it explained very well how to make this game work
[code=python]
so_far = "-" * len(word)
# create a new so_far to include guess
new = ""
for i in range(len(word) ):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new
[/code]Leave a comment:
-
I tried this way:
[code=python]
>>name = 'John John'
>>letter = 'J'
>>iList = indexList(name, letter)
>>print iList
[0]
#but it should contain
[0,5]
[/code]...Leave a comment:
-
tried but it only gave me a first index occurrence of an itemLeave a comment:
-
string related question
[code=python]
a = 'hello'
for i in range(len(a)):
print a.index('l',a.i ndex('l')+i)
2
3
Traceback (most recent call last):
File "C:/Python25/test.py", line 4, in <module>
print a.index('l',a.i ndex('l'))
ValueError: substring not found
[/code]
I would like to find all indexes of letter...Leave a comment:
-
The codes people used in those threats were so complex. So, I need to break the whole thing into small pieces and work on each one.
My first question is how to reveal the * which representes for a hidden letter.
[code=python]
#when the program it running
>> play('python')
* * * * * *
the hidden word has 6 letters
turn: 5
Please enter a letter: p
...Leave a comment:
-
write a Hangman game
I've seen several versions of Hangman game written in many different computer languages like Pascal, C++, Java and even in my TI calculator ( I don't know what the code mean, but what kind of computer languages developers use). I'm really interesting in written this game using Python, however, I don't know how to start. Can someone help me write the simple version of this game with just a few secret words (without the picture of the man is being...
No activity results to display
Show More
Leave a comment: