Python code to count integers based on length and starting digit - python

I have a list of numbers of varying lengths stored in a file, like this...
98
132145
132324848
4435012341
1254545221
2314565447
I need a function that looks through the list and counts every number that is 10 digits in length and begins with the number 1. I have stored the list in both a .txt and a .csv with no luck. I think a big part of the problem is that the numbers are integers, not strings.
`import regex
with open(r"C:\Desktop\file.csv") as file:
data = file.read()
x = regex.findall('\d+', data)
def filterNumberOne(n):
if(len(n)==10:
for i in n:
if(i.startswith(1)):
return True
else:
return False
one = list(filter(filterNumberOne, x))
print(len(one))`

You could simply do like this :
# Get your file content as a string.
with open(r"C:\Desktop\file.csv") as f:
s = " ".join([l.rstrip("\n").strip() for l in f])
# Look for the 10 digits number starting with a one.
nb = [n for n in s.split(' ') if len(n)==10 and n[0]=='1']
In your case, the output will be:
['1254545221']

So I ended up using the following, seems to work great..
def filterNumberOne(n):
if (len(n)==10:
if str(n)[0] == '1':
return True
else:
return False
one = list(filter(filterNumberOne, x ))
print(len(one))

Related

Unable to extract numbers and sum them using regex and re.findall()

I try to extract numbers from a text file with regex. Afterward, I create the sum.
Here is the code:
import re
def main():
sum = 0
numbers = []
name = input("Enter file:")
if len(name) < 1 : name = "sample.txt"
handle = open(name)
for line in handle:
storage = line.split(" ")
for number in storage:
check = re.findall('([0-9]+)',number)
if check:
numbers.append(check)
print(numbers)
print(len(numbers))
for number in numbers:
x = ''.join(number)
num = int(x)
sum = sum + num
print(sum)
if __name__ == "__main__":
main()
The problem is, if this string "http://www.py4e.com/code3/"
I gets add as [4,3] into the list and later summed up as 43.
Any idea how I can fix that?
I think you just change numbers.append(check) into numbers.extend(check)because you want to add elements to an array. You have to use extend() function.
More, you do not need to use ( ) in your regex.
I also tried to check code on python.
import re
sum = 0;
strings = [
'http://www.py4e.com/code3/',
'http://www.py1e.com/code2/'
];
numbers = [];
for string in strings:
check = re.findall('[0-9]+', string);
if check:
numbers.extend(check)
for number in numbers:
x = ''.join(number)
num = int(x)
sum = sum + num
print(sum)
I am assuming instead of 43 you want to get 7
The number variable is an array of characters. So when you use join it becomes a string.
So instead of doing this you can either use a loop in to iterate through this array and covert elements of this array into int and then add to the sum.
Or
you can do this
import np
number np.array(number).astype('int').tolist()
This makes array of character into array on integers if conversion if possible for all the elements is possible.
When I add the string http://www.py4e.com/code3/" instead of calling a file which is not handled correctly in your code above fyi. The logic regex is running through two FOR loops and placing each value and it's own list[[4],[3]]. The output works when it is stepped through I think you issue is with methods of importing a file in the first statement. I replaced the file with the a string you asked about"http://www.py4e.com/code3/" you can find a running code here.
pyregx linkhttps://repl.it/join/cxercdju-shaunpritchard
I ran this method below calling a string with the number list and it worked fine?
#### Final conditional loop
``` for number in numbers:
x = ''.join(number)
num = int(x)
sum = sum + num
print(str(sum)) ```
You could also try using range or map:
for i in range(0, len(numbers)):
sum = sum + numbers
print(str(sum))

Taking single value input in Python

I was attempting to do one kata problem in Python, where I have two lists of input, let's say weight and value, which was getting input in the order (value1, weight1, value2, weight2,....)
If it were C++, I could just take one element at a time with cin to my arrays. But with Python I don't know how to take this input.
If the input is like
60 10 100 20 120 30 (in a single line)
I want my two lists val and w have values
val=[60,100,120]
w=[10,20,30]
How to take this kind of inputs in Python? Is it possible to read only one input at a time, like cin does in C++?
You can read space-separated input to a list using splitand then use slicing to get the odd/even indexed elements:
val = input().split()
val = [int(i) for i in val] #to integer
w = val[1::2] # get only odd indices
val = val[0::2] # get only even indices
print(val) # [60,100,120]
print(w) # [10,20,30]
You can then use regular indexing to get individual elements in the lists.
This should do it.
values = input("Enter values: ").split(" ")
if(len(values) % 2 != 0):
print("Error, uneven number of values and weights")
else:
val = []
w = []
for i in range(0, len(values) - 1, 2):
val.append(values[i])
w.append(values[i+1])
print("val: ", val)
print("w: ", w)
No you cannot as far as I know. input() is a function that reads one line at a time as an entire string.
A popular way to make a list out of an entire line of space separated integers is using the in-built map() function and split() method of a string object:
numbers = map(int, input.split())
numbers will now contain the list of numbers on that line.
Firstly, input() reads the entire line as a string including spaces.
split() function splits the input at space, you can also pass in a string argument to make it split anywhere you want, i.e a custom delimiter like , and creates a list of strings of those numbers
map() calls int() on each of the numbers in that list.
In C++, just like you need to know the size of input in advance, you can run a for loop on the list to now split values and weights into two lists.
val, w = [], []
for i in range(len(numbers)):
if i % 2 == 0: val.append(numbers[i])
else: w.append(numbers[i])
For better performance(relevant for large input size), you can even skip the map() step, and do something like this:
numbers = input().split()
val, w = [], []
for i in range(len(numbers)):
if i % 2 == 0: val.append(int(numbers[i]))
else: w.append(int(numbers[i]))
Here's a simple solution:
a = "60 10 100 20 120 30"
split_a = a.split(" ")
val = [e for (i, e) in enumerate(split_a) if i % 2 == 0]
w = [e for (i, e) in enumerate(split_a) if i % 2 == 1]
# or shorter with slicing (start:stop:step)
val = [e for e in split_a[0::2]]
w = [e for e in split_a[1::2]]
I am not sure what cin does but I assume the input you have is a string. One way you could do is to split it into a list of ints. And then create your val and w lists and append your values into the lists. (The first, third, fifth, etc would be your val list)
my_input_list = my_input.split(' ')
Read more here.

How to find the largest set of consecutive integers in python?

I'm looking at a very large set of binary data which is in a separate file. The challenge is to find the largest consecutive number of 1's and 0's. I have already accessed the file from within Python (I'm using Python btw) and have been able to code to find out the total number of 0's and 1's. any help would be much appreciated as I am a total beginner to coding when using Python. Cheers.
This what I've done thus far:
filename = "C:/01.txt"
file = open(filename, "r")
count_1 = 0
count_0 = 0
for line in file:
count_0 = count_0 + line.count("0")
count_1 = count_1 + line.count("1")
pass
print("Number of 1s = " + str(count_1))
print("Number of 0s = " + str(count_0))
I have not actually started the coding to find the consecutive numbers.
To find the longest occurrence of a certain substring, you could use a function like this one:
def longest_segment(sub, string):
return max(m.group() for m in re.finditer(r'(%s)\1*' % sub, string))
This works by finding all occurrences of the provided substring, sub, in the string, and returning the longest one.
Here is a simple solution: Loop through the data, count consecutive 1s read and when reading a 0 (meaning you reached the end of one segment) compare it's length to the longest segment of consecutive 1s found so far.
def getMaxSegmentLength(readable):
current_length= 0
max_length= 0
for x in readable:
if x == '1':
current_length+= 1
else:
max_length= max(max_length, current_length)
current_length= 0
return max(max_length, current_length)
def main():
# open a file located in G:/input.txt in read mode and name the file object: inputf
with open('G:/input.txt', 'r') as inputf:
# put all the text in filef in s
s= inputf.read()
# get the longest streak of 1s in string s
n= getMaxSegmentLength(s)
print(n)
if __name__ == '__main__':
main()
s=raw_input() #read s from file in this case
zero=0
one=0
zz=0
oo=0
for i in list(s):
if i=='1':
if zz>=1:
zero=max(zero,zz)
zz=0
oo+=1
else:
if oo>=1:
one=max(one,oo)
oo=0
zz+=1
if oo>=1:
one=max(oo,one)
if zz>=1:
zero=max(zero,zz)
print zero,one
#O(n)

I want to write a code that can take two numbers and create a txt file between those numbers in decreasing order

here is the code
import os
myfile = ('filename.txt' ,'wb')
a = int(input('integer a: '))
b = int(input('integer b: '))
def cal(a,b):
while a>b:
a = a-1
print a
c = str(cal(a,b))
print c
myfile.writelines(c)
I am getting only last word written in file. please someone help me.
Even if you returned a at the end of your cal function you would just get one number being written. You can do the whole thing a lot easier by using a range and converting it to a list, then joining that list with a space
import os
smallNum= int(input('smaller integer a: '))
bigNum= int(input('bigger integer b: '))
with open('filename.txt' ,'w') as myFile:
myFile.write(" ".join([str(x) for x in list(range(smallNum+1,bigNum))]))
This line of code creates a range of all the numbers in between your two nums:
range(bigNum+1,smallNum)
Then convert to a list (of ints)
list(range(bigNum+1,smallNum))
Then convert that to a list of strings with a list comprehension
[str(x) for x in list(range(bigNum+1,smallNum))]
Finally join each item in the list with a space
" ".join([str(x) for x in list(range(bigNum+1,smallNum))])
A solution that doesn't use list comprehensions / .join / context managers
import os
smallNum = int(input('smaller integer a: '))
bigNum = int(input('bigger integer b: '))
writeString = "" #string to write to file later
for currentNum in range(smallNum+1, bigNum): #non-inclusive range of numbers
strNum = str(currentNum) #convert to string
writeString += strNum + " " #add the number and a space to the string
file = open("filename.txt", "w")
file.write(writeString)
file.close()

Stuck on Project Euler # 13

Please help me understand why my Python code to solve the 13th Project Euler problem is incorrect. I believe that I understand the task correctly and I think my code is correct, but it is obviously not.
number = '5000 digit number - see in the problem decription at the provided link'
list1 = [number[i:i+100] for i in range(0, len(number), 100)]
temp = []
for i in range(0, len(list1)):
y = int(list1[i])
temp.append(y)
print sum(temp)
First, the numbers are 50 digits long, not 100. Change this:
list1 = [number[i:i+100] for i in range(0,len(number),100)]
To this:
list1 = [number[i:i+50] for i in range(0,len(number),50)]
Second, you're printing the entire sum, rather than just the first ten digits. Try:
print str(sum(temp))[:10]
Much simpler:
s = 'copied and pasted from the page'
result = sum(map(int, s.splitlines()))[:10]
Only the 11 first digits need to be summed,
somme11=sum(int(number2[i:i+11]) for i in range(100))
print(somme11)
print( 'the ten first digits are' , somme11//1000)
Because the carry can't exceed 99.
4893024188690
the ten first digits are 4893024188
An alternative is to load the numbers into a file and then just add them up, unless I too have completely misunderstood the question.
with open("sum.nums","r") as f:
data = f.readlines()
total = 0
for i in data:
total += int(i)
print "1st 10 digits ", str(total)[:10], "of total", total
1st 10 digits 5537376230 of total 5537376230390876637302048746832985971773659831892672
It's pretty simple
def e13():
f=open("100x50digits.txt")
summ=0
for line in f:
summ+=int(line[:11])
print(int(summ/1000))
f.close()
e13()
number = '''The 100 lines of 50 characters'''
numbers = number.splitlines() #Devide the string into a list of strings.
numbers = [int(i) for i in numbers] #Convert all elements into integers.
totalSum = str(sum(numbers)) #Add everything and convert to a string.
print(totalSum[:10]) #Print the first 10 digits of the result.
The correct answer is 5537376230, checked with P.E.
The real challenge here is to deal with the really long string
Use StringIO to take number as a input string and iterate trough elements by converting each one to a integer value
from io import StringIO
number = StringIO(""" # Paste one-hundred 50 numbers """)
print(str(sum(map(lambda x: int(x), number)))[:10])
>>> '5537376230'

Categories

Resources