I saw this code on the internet:
s=input().count
print( max( (s('6')+s('9')+1)//2, max([s(i) for i in "01234578"])))
but I don't get what this line does :
s=input().count
I thought this function was to count how many letters are in the word. So I tried to print the s, but I got this error:
<built-in method count of str object at 0x7f4f8b859148>
what does input().count function do and what situation will it be used?
s=input().count is a function which you can call.
You could write
input().count('6')
to count how many times you get 6 in the input.
Or,
s('6')
is now a shorthand for this.
s = input().count
Basically you are binding the count method to s so when you call s like s(i), it converts the code to input().count(i) internally.
Let me give you a simple example:
p = print
p("hello world")
# hello world
Related
word = input().upper()
word_list = list(set(word))
cnt = []
for i in word_list:
count = word.count
cnt.append(count(i))
if cnt.count(max(cnt)) > 1:
print("?")
else:
print(word_list[(cnt.index(max(cnt)))])
in this code, look at the line6, count=word.count, count function is used.
I know that count function must be used like '.count()' this formation.
But in this case, '' is not used.
So I want to know how it is possible and how it can run very well.
+) when I print(type(word.count)), <built-in method count of str object at 0x01DD8800> is returned!
word.count is a bound method of word. Normally, you'd bind the method and then call it immediately with word.count(i), but it's perfectly legal to bind the method (attaching the instance to it for when it's called in the future), then call it at some later point. That's all you did here, you made count equivalent to word.count, then called it later providing the argument it required, as if you'd called word.count(i) at that point.
When you do something like count = word.count() what you are doing is calling the word.count function and assign the value that the function returns to the count variable. When you do count = word.count what you are really doing is assing the function pointer to the count variable so now count points to the same thing as word.count and you can now use it call that with count() and now it is basically the same thing as word.count() and if you do print(word.count) and print(count) it will give you the same output in both saying you the pointer of the function
So, I have the following function which should resemble the already implemented " print " function in Python. ( I know it is silly to make a function that only uses a pre-defined function but I am practicing with different things ) The thing that I want to do with my function is: make it act the same as the pre-defined one. That is, when the print function is called with no parameters, I would like it to print an empty line just as " print() " does. How do i do that ?
def print_str( string, how_many_times, tail ):
print ( string * how_many_times, end = tail )
print doesn't take a single string, it takes 0 or most strings as argument. end has a reasonable default. You can imagine a (simplified) definition of
def print(*args, end='\n'):
...
You need to do the same for yours. A reasonable default for how_many_times would be 1. If you don't want print_str to take multiple arguments, "" would be a reasonable default for string.
def print_str(string="", how_many_times=1, tail='\n'):
print(string * how_many_times, end=tail)
You can do something like this:
def myfunc(a=None):
if a == None:
print()
else:
print("hi")
If no arguments are presented it prints nothing, but if an argument is given it prints 'hi'.
This might be python 101, but I am having a hard time changing letters into a valid integer.
The put what I am trying to do simply
char >> [ ] >> int
I created a case statement to give me a number depending on certain characters, so what I tried doing was
def char_to_int(sometext):
return {
'Z':1,
'Y':17,
'X':8,
'w':4,
}.get(sometext, '')
Which converts the letter into a number, but when I try using that number into any argument that takes ints it doesn't work.
I've tried
text_number = int(sometext)
But I get the message TypeError: int() argument must be a string or a number, not 'function'
So from there I returned the type of sometext using
print(type(sometext))
And the return type is a function.
So my question is, is there a better way to convert letters into numbers, or a better way to setup my switch/def statement
Heres the full code where its call
if sometext:
for i in range ( 0, len(sometext)):
char_to_int(sometext[i])
I've managed to get it working, ultimately what I changed was the default of the definition, I now set the definition to a variable before instead of calling it in another function, and I recoded the section I was using it.
Originally my definition looked liked this
def char_to_int(sometext):
return {
...
}.get(sometext, '')
But I changed the default to 0, so now it looks like
def char_to_int(sometext):
return {
...
}.get(sometext, 0)
The old code that called the definition looked
if sometext:
for i in range ( 0, len(sometext)):
C_T_I = int(char_to_int(sometext[i]))
I changed it to this.
if sometext:
for i in range ( 0, len(sometext)):
C_T_I = char_to_int(sometext[i])
TEXTNUM = int(C_T_I)
Hopefully this clarifies the changes. Thanks for everyone's assistance.
in the python console:
>>> type({ 'Z':1, 'Y':17, 'X':8, 'w':4, }.get('X', ''))
<class 'int'>
so as cdarke suggested, you should look at how you are calling the function.
I am kinda new to Python, and I would like to ask a question :
def spam(a):
a = 1 + a
return a
spam(21)
print(spam)
input()
After running it, the output is function spam at 0x021B24F8. Shouldn't the output be 22? Any help will be appreciated.
The problem is that your function, i.e. spam is returning a value. You need to accept the value returned by the function and store it in a different variable as in
s = spam(21)
print(s)
Here, you will store the returning value in the variable s which you will print it out.
After making the correction, the program will print as expected as in
22
Note - As mentioned, having a single statement print(spam(21)) also works as spam(21) will return 22 to the print function which will then print out the value for you!
I'm watching the instructional videos on you youtube and started doing some of the exercises at http://code.google.com/edu/languages/google-python-class but I'm puzzled by the below problem in the string1.py file.
What I can't seem to understand is, what is the "s" in both_ends(s): doing?
# B. both_ends
# Given a string s, return a string made of the first 2
# and the last 2 chars of the original string,
# so 'spring' yields 'spng'. However, if the string length
# is less than 2, return instead the empty string.
def both_ends(s):
# +++your code here+++
# LAB(begin solution)
if len(s) < 2:
return ''
first2 = s[0:2]
last2 = s[-2:]
return first2 + last2
At the bottom of strings1.py there are some functions:
def main()
print 'both_ends'
test(both_ends('spring'), 'spng')
if __name__ == '__main__':
main()
So how does the program know to substitute "spring" for (s) or is that not what it's doing? I can post the entire file if need be. It's only 140 lines.
'spring' is the literal string passed as a parameter into function both_ends(), and 's' is the formal parameter to the function. Replacing a formal parameter with an actual parameter is performed when the function is called.
The 'test()' function is just there to confirm that the function behaves as expected.
When you call a function, the values you give the function are assigned to the corresponding arguments in the function header. In code:
def my_func(a): #function header; first argument is called a.
#a is not a string, but a variable.
print a #do something with the argument
my_func(20) #calling my_func with a value of 20. 20 is assigned to a in the
#body of the function.
s is a variable that we presume to hold a string. We pass 'spring' in through as a parameter.
s in def both_ends(s) is the parameter for the input string. The length of this string is checked with the call to len(s) < 2, and various characters in the string are accessed by position with s[0:2] and s[-2:]
See http://docs.python.org/tutorial/controlflow.html#defining-functions for specifics. Also the tutorial at http://docs.python.org/tutorial/index.html is pretty good - I mostly learnt from it.
s is the parameter to the function, but you plug in real strings like hello or world into the function instead of just the letter s. Think of it like a math function: you have f(x) = x + 5. When you plug in a number, say 2, you get f(2) = 2 + 5. That's exactly what happens with the both_ends function. To make it simpler, here's some code:
def f(x):
return x + 5
f(2)
The way you plug into the function in the code here is the same way you plug a string into your original function.