Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
Please help me understand how key variable can have a value a or b?
from collections import defaultdict
dict = defaultdict(list)
group_a_count, group_b_count = map(int, input().split())
for i in range(1, group_a_count + 1):
dict[input()].append(i)
for i in range(1, group_b_count + 1):
key = input()
print(key)
INPUT:
5 2
a
a
b
a
b
a
b
OUTPUT:
a
b
All input values was appended to dictonary in first cycle. And how second cycle understood where to get keys from the dictonary?
It's a bit messy code. The only thing the second loop does, is asking group_b_count times an input, which it then prints and continues with the next iteration... So the output is a and b because of this piece of code:
key = input()
print(key)
which just prints what you gave as input
What's the purpose of this program actually?
And if i use for cycle with this parameters:
for i in range(1, 5):
key = input()
print(key)
in STDOUT will be still a and b. A full purpose of program: "In this challenge, you will be given 2 integers, n and m. There are n words, which might repeat, in word group A. There are m words belonging to word group B. For each m words, check whether the word has appeared in group A or not. Print the indices of each occurrence of m in group A. If it does not appear, print -1."
I misunderstood task from HackerRank and I expected another output.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
def spell(txt):
txt = input()
spell(txt)
Given a string as input, use recursion to output each letter of the strings in reverse order, on a new line.
Sample Input
HELLO
Sample Output
O
L
L
E
H
I used recursion to get output in reverse order, but didn't get exactly.
Working of spell() function:
The function works by first checking if the length of the string is 1. If it is, the function simply prints the string (since it's the last letter of the original string, and therefore the first one to be printed in reverse order). If the length of the string is greater than 1, the function prints the last letter of the string (which is the next letter to be printed in reverse order), and then calls itself recursively with the string sliced to exclude the last letter.
Code:
def spell(txt):
if len(txt) == 1:
print(txt)
else:
print(txt[-1])
spell(txt[:-1]) #sliced the last character and spell function is call again.
spell(input("Input:\n"))
Output:
Input:
HELLO
O
L
L
E
H
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In this code first it take d=0 and l = 0 and then it write d=d+1 and l=l+1 do in this code what its uses in this code ? ]1
In this code first it take d=0 and l = 0 and then it write d=d+1 and l=l+1 do in this code what its uses in this code ?
Go through it line by line - first you run a loop where each character of the input is sequentially represented as i.
Now, for each i, if i is a digit, you increase the count of d - using d=d+1.Otherwise (elif) if i is an alphabet, you increase the count of l - using l=l+1.
This way you're storing the number of digits in the input as d, and the number of letters in the input as l.
Finally, after the loop runs on all character of the input, you print the number of letters and digits respectively using print("letter",l,"digit",d)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create this kind of output in Python
["k", "kk", "kkk", "kkkk", ...]
["rep", "reprep", "repreprep", ...]
That is a list of n elements, made of the same character (or small group of characters) repeated X times, X being increased by one for each element.
I can't find a way to do this easily, without loops..
Thanks,
Here you have a generator using itertools.count, remember the property of "multiplying" strings in python by a number, where they will be replicated and concatenated nth times, where for example "a"*3 == "aaa" :
import itertools
def genSeq(item):
yield from (item*i for i in itertools.count())
Here you have a live example
repeating_value = "k" #Assign the value which do you want to be repeated
total_times=5 #how many times do you want
expected_list=[repeating_value*i for i in range(1,total_times+1)]
print(expected_list)
character = 'k'
_range = 5
output = [k*x for k in character for x in range(1, _range + 1)]
print(output)
I would multiple my character by a specified number in the range, and then I would simply iterate through the range in a list comprehension. We add 1 to the end of the range in order to get the full range.
Here is your output:
['k', 'kk', 'kkk', 'kkkk', 'kkkkk']
The following is by far the easiest which I have built upon the comment by the user3483203 which eliminates initial empty value.
var = 'rep'
list = [var * i for i in range(1,x,1)]
print(list)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create a Python program that will count how many positive numbers there are in a list of numbers. The list of numbers has to be typed in by someone. The end result must be the number of elements in the list that were > 0
For an example, this is what you would see on the screen:
>>>Please enter a list of numbers separated by commas: 1,2,-3,-4,5,-6
>>>3
The answer would be 3 in this example. I am sorry if the question seems stupid, but I am a beginner and I am trying my best.
raw_input() for Python 2.x (input() for Python 3) then split() the string at , and then count positive numebers, Example -
s = raw_input("Please enter a list of numbers separated by commas:")
print(len([i for i in s.strip().split(',') if int(i) >= 0]))
You can try like this. input returns tuple
>>> vals = input('get: ')
get: 1,2,-3,-4,5,-6
>>> len([item for item in vals if item > 0])
3
Python 3, input returns string
>>> vals = input('get: ')
get: 1,2,-3,-4,5,-6
>>> len([item for item in vals.split(',') if int(item) > 0])
3
By the way, zero is neither positive nor negative.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Please help (I know that it's a silly question):
I have a list d = [' ABA', ' AAB', ' BAA', ' BAA', ' AAB', ' ABA']. How can I exclude elements that appear more than once?
To exclude items from the list that appear more than once:
d = [x for x in d if d.count(x) == 1]
For the example provided above, d will bind to an empty list.
Others have posted good solutions to remove duplicates.
Convert to a set then back again:
list(set(d))
If order matters, you can pass the values through a dict that remembers the original indices. This approach, while expressible as a single expression, is considerably more complicated:
[x for (i, x) in sorted((i, x) for (x, i) in dict((x, i) for (i, x) in reversed(list(enumerate(d)))).iteritems())]
Of course, you don't have to use comprehensions. For this problem, a fairly simple solution is available:
a = []
for x in d:
if x not in a:
a.append(x)
Note that both the order-preserving solutions assume that you want to keep the first occurrence of each duplicated element.
Lets say you got a list named Words and a list UniqueWords, start a loop on Words, on each iteration you check if the list UniqueWords contains the iterated element, if so then continue, if not then add it to the UniqueWords. In the end you will have a list without duplicates. Another way you could do is a loop in a loop and instead of adding you'd remove it if it was found more than once :)
I bet there are far more efficient ways though.
If you're not worried about the order, d = list(set(d))).
If order matters check out the unique_everseen function in the itertools recpies documentation. It give a relatively clean iterator-based solution.
If order doesn't matter, convert to a set.
If you shouldn't have done that already, make sure to read the python docs on itertools, especially product(), permutations() and combinations().