Python issue with reading and calculating data from excel file - excel

I have attached a screenshot of the excel file I am working with
I am trying to read this excel file which has all the states (column B), Counties (column C) and Population (column D). I want to calculate the population for each state.
I know that there are various ways we all can do it and there is certainly a way to do this in fewer lines of easily understandable code. I will appreciate that but I would also like to know how to do this the way I am thinking which is - to first find out unique state names and then loop through the sheet to add all the columns by state.
Here is my code:
x = wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Population by Census Tract')
PopData = {}
StateData = []
i = 3
j = 0
k=""
#First value entered
StateData.append(sheet['B' + str(2)].value)
#Unique State Values calculated
for row in range(i, sheet.max_row + 1):
if any(sheet['B' + str(row)].value in s for s in StateData):
i=i+1
else:
StateData.append(sheet['B' + str(row)].value)
print(StateData)
#Each State's Population calculated
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[s]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
print(PopData)
I am getting this error:
if sheet['B' + str(row)].value == StateData[s]:
TypeError: list indices must be integers or slices, not str

In the following:
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[s]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
s is already an element of the StateData list. What you want to do is probably:
for s in StateData:
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == s:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
or
for i, s in enumerate(StateData):
for row in range(2, sheet.max_row + 1):
if sheet['B' + str(row)].value == StateData[i]:
j = j + sheet['D' + str(row)].value
PopData[StateData[s]] = j
but the first alternative is more elegant and (maybe) slightly faster.

Related

I am trying to write stuff connected with Lagrange's four-square theorem, how can I optimize my code, because it can't pass all the tests on of time

The task is: Given a positive integer n, find the number of ordered tuples (a, b, c, d, e) over non-negative integers for which a² + b² + c² + d² = n and b + 3c + 5d = e².
n = int(input())
count = 0
for e in range(3 * math.floor(math.sqrt(math.sqrt(n))) + 2):
for c in range(math.floor(math.sqrt(n)) + 1):
for b in range(math.floor(math.sqrt(n)) + 1):
if (e**2-b-3*c)/5 == int((e**2-b-3*c)/5) and (e**2-b-3*c) >= 0:
for a in range(math.floor(math.sqrt(n)) + 1):
if a**2 +(26*b**2+6*b*c-2*e**2*b+34*c**2-6*e**2*c+e**4)/25 == n:
count += 1
print(count)

Count the number of possible ways to solve an equation

This question was asked in a challenge in HackerEarth:
Mark is solving an interesting question. He needs to find out number
of distinct ways such that
(i + 2*j+ k) % (x + y + 2*z) = 0, where 1 <= i,j,k,x,y,z <= N
Help him find it.
Constraints:
1<= T<= 10
1<=N<= 1000
Input Format:
First line contains T, the number of test cases. Each of the test case
contains a single integer,N in a separate line.
Output Format:
For each test case , output in a separate line, the number of distinct
ways.
Sample Input
2
1
2
Sample Output
1
15
Explanation
In the first case, the only possible way is i = j = k = x =y = z = 1
I am not getting any way how to solve this problem, I have tried one and I know it's not even close to the question.
import random
def CountWays (N):
# Write your code here
i = random.uniform(1,N)
j = random.uniform(1,N)
k = random.uniform(1,N)
x = random.uniform(1,N)
y = random.uniform(1,N)
z = random.uniform(1,N)
d = 0
for i in range(N):
if (i+2*j+k)%(x+y+2*z)==0:
d += 1
return d
T = int(input())
for _ in range(T):
N = int(input())
out_ = CountWays(N)
print (out_)
My Output
0
0
Instead it should give the output
1
15
The value of the numerator (num) can range from 4 to 4N. The value of the denominator (dom) can range from 4 to num. You can split your problem into two smaller problems: 1) How many values of the denominator is a given value of the numerator divisible by? 2) How many ways can a given denominator and numerator be constructed?
To answer 1) we can simply loop through all the possible values of the numerator, then loop over all the values of the denominator where numerator % denominator == 0. To answer 2) we can find all the partitions of the numerator and denominator that satisfies the equality and constraints. The number of ways to construct a given numerator and denominator will be the product of the number of partitions of each.
import itertools
def divisible_numbers(n):
"""
Get all numbers with which n is divisible.
"""
for i in range(1,n+1):
if n % i == 0:
yield i
if i >= n:
break
def get_partitions(n):
"""
Generate ALL ways n can be partitioned into 3 integers.
Modified from http://code.activestate.com/recipes/218332-generator-for-integer-partitions/#c9
"""
a = [1]*n
y = -1
v = n
while v > 0:
v -= 1
x = a[v] + 1
while y >= 2 * x:
a[v] = x
y -= x
v += 1
w = v + 1
while x <= y:
a[v] = x
a[w] = y
if w == 2:
yield a[:w + 1]
x += 1
y -= 1
a[v] = x + y
y = a[v] - 1
if w == 3:
yield a[:w]
def get_number_of_valid_partitions(num, N):
"""
Get number of valid partitions of num, given that
num = i + j + 2k, and that 1<=i,j,k<=N
"""
n = 0
for partition in get_partitions(num):
# This can be done a bit more cleverly, but makes
# the code extremely complicated to read, so
# instead we just brute force the 6 combinations,
# ignoring non-unique permutations using a set
for i,j,k in set(itertools.permutations(partition)):
if i <= N and j <= N and k <= 2*N and k % 2 == 0:
n += 1
return n
def get_number_of_combinations(N):
"""
Get number of ways the equality can be solved under the given constraints
"""
out = 0
# Create a dictionary of number of valid partitions
# for all numerator values we can encounter
n_valid_partitions = {i: get_number_of_valid_partitions(i, N) for i in range(1,4*N+1)}
for numerator in range(4,4*N+1):
numerator_permutations = n_valid_partitions[numerator]
for denominator in divisible_numbers(numerator):
denominator_permutations = n_valid_partitions[denominator]
if denominator < 4:
continue
out += numerator_permutations * denominator_permutations
return out
N = 2
out = get_number_of_combinations(N)
print(out)
The scaling of the code right now is very poor due to the way the get_partitions and the get_number_of_valid_partitions functions interact.
EDIT
The following code is much faster. There's a small improvement to divisible_numbers, but the main speedup lies in get_number_of_valid_partitions not creating a needless amount of temporary lists as it has now been joined with get_partitions in a single function. Other big speedups comes from using numba. The code of get_number_of_valid_partitions is all but unreadable now, so I've added a much simpler but slightly slower version named get_number_of_valid_partitions_simple so you can understand what is going on in the complicated function.
import numba
#numba.njit
def divisible_numbers(n):
"""
Get all numbers with which n is divisible.
Modified from·
"""
# We can save some time by only looking at
# values up to n/2
for i in range(4,n//2+1):
if n % i == 0:
yield i
yield n
def get_number_of_combinations(N):
"""
Get number of ways the equality can be solved under the given constraints
"""
out = 0
# Create a dictionary of number of valid partitions
# for all numerator values we can encounter
n_valid_partitions = {i: get_number_of_valid_partitions(i, N) for i in range(4,4*N+1)}
for numerator in range(4,4*N+1):
numerator_permutations = n_valid_partitions[numerator]
for denominator in divisible_numbers(numerator):
if denominator < 4:
continue
denominator_permutations = n_valid_partitions[denominator]
out += numerator_permutations * denominator_permutations
return out
#numba.njit
def get_number_of_valid_partitions(num, N):
"""
Get number of valid partitions of num, given that
num = i + j + 2l, and that 1<=i,j,l<=N.
"""
count = 0
# In the following, k = 2*l
#There's different cases for i,j,k that we can treat separately
# to give some speedup due to symmetry.
#i,j can be even or odd. k <= N or N < k <= 2N.
# Some combinations only possible if num is even/odd
# num is even
if num % 2 == 0:
# i,j odd, k <= 2N
k_min = max(2, num - 2 * (N - (N + 1) % 2))
k_max = min(2 * N, num - 2)
for k in range(k_min, k_max + 1, 2):
# only look at i<=j
i_min = max(1, num - k - N + (N + 1) % 2)
i_max = min(N, (num - k)//2)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
# if i == j, only one permutations
# otherwise two due to symmetry
if i == j:
count += 1
else:
count += 2
# i,j even, k <= N
# only look at k<=i<=j
k_min = max(2, num - 2 * (N - N % 2))
k_max = min(N, num // 3)
for k in range(k_min, k_max + 1, 2):
i_min = max(k, num - k - N + N % 2)
i_max = min(N, (num - k) // 2)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
if i == j == k:
# if i == j == k, only one permutation
count += 1
elif i == j or i == k or j == k:
# if only two of i,j,k are the same there are 3 permutations
count += 3
else:
# if all differ, there are six permutations
count += 6
# i,j even, N < k <= 2N
k_min = max(N + 1 + (N + 1) % 2, num - 2 * N)
k_max = min(2 * N, num - 4)
for k in range(k_min, k_max + 1, 2):
# only look for i<=j
i_min = max(2, num - k - N + 1 - (N + 1) % 2)
i_max = min(N, (num - k) // 2)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
if i == j:
# if i == j, only one permutation
count += 1
else:
# if all differ, there are two permutations
count += 2
# num is odd
else:
# one of i,j is even, the other is odd. k <= N
# We assume that j is odd, k<=i and correct the symmetry in the counts
k_min = max(2, num - 2 * N + 1)
k_max = min(N, (num - 1) // 2)
for k in range(k_min, k_max + 1, 2):
i_min = max(k, num - k - N + 1 - N % 2)
i_max = min(N, num - k - 1)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
if i == k:
# if i == j, two permutations
count += 2
else:
# if i and k differ, there are four permutations
count += 4
# one of i,j is even, the other is odd. N < k <= 2N
# We assume that j is odd and correct the symmetry in the counts
k_min = max(N + 1 + (N + 1) % 2, num - 2 * N + 1)
k_max = min(2 * N, num - 3)
for k in range(k_min, k_max + 1, 2):
i_min = max(2, num - k - N + (N + 1) % 2)
i_max = min(N, num - k - 1)
for i in range(i_min, i_max + 1, 2):
j = num - i - k
count += 2
return count
#numba.njit
def get_number_of_valid_partitions_simple(num, N):
"""
Simpler but slower version of 'get_number_of_valid_partitions'
"""
count = 0
for k in range(2, 2 * N + 1, 2):
for i in range(1, N + 1):
j = num - i - k
if 1 <= j <= N:
count += 1
return count
if __name__ == "__main__":
N = int(sys.argv[1])
out = get_number_of_combinations(N)
print(out)
The current issue with your code is that you've picked random numbers once, then calculate the same equation N times.
I assume you wanted to generate 1..N for each individual variable, which would require 6 nested loops from 1..N, for each variable
Now, that's the brute force solution, which probably fails on large N values, so as I commented, there's some trick to find the multiples of the right side of the modulo, then check if the left side portion is contained in that list. That would only require two triple nested lists, I think
(i + 2*j+ k) % (x + y + 2*z) = 0, where 1 <= i,j,k,x,y,z <= N
(2*j + i + k) is a multiple of (2*z + x + y)
N = 2
min(2*j + i + k) = 4
max(2*j + i + k) = 8
ways to make 4: 1 * 1 = 1
ways to make 5: 2 * 2 = 4
ways to make 6: 2 * 2 = 4
ways to make 7: 2 * 2 = 4
ways to make 8: 1 * 1 = 1
Total = 14
But 8 is a multiple of 4 so we add one more instance for a total of 15.

Where should I put the count in this tim sort algorithm, to accurately compare runtime to other algorithms

I've written a Timsort sorting algorithm for a computer science class, I would like to be able to compare the runtime to other similar algorithms, such as merge sort for instance. However, I am not sure where I should put the count (ie: count +=1)within the code to have an accurate run time. Any help would be much appreciated.
RUN = 32
def insertion_sort(arr, left, right):
for i in range(left + 1, right + 1):
temp = arr[i]
j = i - 1
while (arr[j] > temp and j >= left):
arr[j + 1] = arr[j]
arr[j] = temp
j -= 1
def merge(arr, left, right, count):
c = 0
index = count
length = len(left) + len(right)
while left and right:
if left[0] < right[0]:
arr[index] = left.pop(0)
c += 1
index += 1
else:
arr[index] = right.pop(0)
c += 1
index += 1
if len(left) == 0:
while c < length:
arr[index] = right.pop(0)
c += 1
index += 1
elif len(right) == 0:
while c < length:
arr[index] = left.pop(0)
c += 1
index += 1
def tim_sort(arr):
n = len(arr)
for i in range(0, n, RUN):
insertion_sort(arr, i, min((i + (RUN - 1)), (n - 1)))
size = RUN
while size < n:
for left in range(0, n, 2 * size):
if (left + size > n):
merge(arr, arr[left:n], [], left)
else:
left_sub_arr = arr[left:(left + size)]
right_sub_arr = arr[(left + size):min((left + 2 * size), n)]
merge(arr, left_sub_arr, right_sub_arr, left)
size *= 2
return arr

Created simple table with for-loop python

Im just trying to create simple table with following this code
n = 0
x = ["list1","list2","list3","list4"]
for y in x:
n+=1
h="{}. {}".format(n,y) + ' ' + "{}. {}".format(n+n,y)
print(h)
And output i got
1. list1 5. list1
2. list2 6. list2
3. list3 7. list3
4. list4 8. list4
What should i do to got output like this
1. list1 3. list3
2. list2 4. list4
Im just beginning and so confused to solved it,thanks for who can answer my issue.
The below should give you what you are looking for, no matter how long the list is. There may be a more efficient way than this but this way breaks it down into its parts, so essentially you need to find the halfway point in the list and print the respective value on the right.
x = ["list1","list2","list3","list4", "list5",]
if len(x) % 2 == 1 :
half = int((len(x) + 1) / 2)
else:
half = int(len(x) / 2)
for i in range(half):
if half > len(x) / 2:
if i == half - 1:
h = "{}. {}".format (i + 1 , x[i])
else:
h = "{}. {}".format (i + 1 , x[i]) + ' ' + "{}. {}".format (half + (i + 1) , x[int (len (x) / 2) + i + 1])
else:
h = "{}. {}".format (i + 1 , x[i]) + ' ' + "{}. {}".format (half + (i + 1), x[int (len (x) / 2) + i])
print(h)
I have also taken into account lists that have an odd number of elements like the above example for x, which will give you the following output
1. list1 4. list4
2. list2 5. list5
3. list3
Something like this?
import math
x = ["list1","list2","list3","list4", "list5", "list6", "list7"]
y = math.ceil(len(x)/2)
for i, j in enumerate(x[:math.ceil(len(x)/2)], start=1):
try:
print("{}. {}".format(i, j) + ' '"{}. {}".format(i+math.ceil(len(x)/2), x[y]))
except:
print("{}. {}".format(i, j))
y +=1
Output:
1. list1 5. list5
2. list2 6. list6
3. list3 7. list7
4. list4

Python Simpson's Rule Float object error

I am having problem with the simpsons rule. It is saying float object cannot be interpreted as an integer in for i in range(1, (n/2) + 1):
def simpson(f, a, b, n):
h=(b-a)/n
k=0.0
x= a + h
for i in range(1, (n/2) + 1):
k += 4*f(x)
x += 2*h
x = a + 2*h
for i in range(1, n/2):
k +=2*f(x)
x += 2*h
return (h/3)*(f(a)+f(b)+k)
result=simpson(lambda x:x, 0, 1, 4)
print (result)
n / 2 returns a float in Python 3, while range only works with integers. You need to use integer division (//):
range(1, (n // 2) + 1).

Resources