Posts: 11
Threads: 3
Joined: Jul 2018
I have been struggling with this code for some time. I have created a dictionary to be used within python but I want to be able to take that dictionary from the following code and be able to make a .txt file or .csv file and be able to place that data into an excel file to be able to look at and examine all the data. Can anyone help me figure this one out?
Thank you in advance
import pickle
def getAllCombinations(s, length):
if length == 0:
return ['']
else:
ret = []
for idx, c in enumerate(s):
combos = getAllCombinations(s[0:idx] + s[idx+1:], length - 1)
for i in range(len(combos)):
combos[i] = c + combos[i]
ret += combos
return ret
def getPermutations(s, ret, swapIdx = 0):
if swapIdx == len(s):
ret.append(int(''.join(s)))
for i in range(swapIdx, len(s)):
cpy = [c for c in s]
cpy[swapIdx], cpy[i] = cpy[i], cpy[swapIdx]
getPermutations(cpy, ret, swapIdx + 1)
def getAllPermutations(i):
s = str(i)
allPerms = set()
for i in range(len(s)):
curCombos = getAllCombinations(s, i + 1)
for combo in curCombos:
ret = []
getPermutations(combo, ret)
allPerms = allPerms.union(set(ret))
return list(allPerms)
def isPrime(n):
if n==2 or n==3: return True
if n%2==0 or n<2: return False
for i in range(3,int(n**0.5)+1,2): # only odd numbers
if n%i==0:
return False
return True
def getNumPrimes(i):
perms = getAllPermutations(i)
numprimes = 0
for perm in perms:
if (isPrime(perm)):
numprimes += 1
return numprimes
def find_maxPrimes():
max = 0
maxNum = 0
for i in range(1000, 10000):
cur = getNumPrimes(i)
if (max < cur):
max = cur
maxNum = i
return (maxNum)
def make_dict():
# max = 0
# maxNum = 0
#Printing here directly
Dict = {}
for i in range(1000, 10000):
Dict[i] = getNumPrimes(i)
return Dict
def make_db(name):
db = make_dict()
pickle_temp = open(f'{name}', 'wb')
pickle.dump(db,pickle_temp)
pickle_temp.close()
def open_db(name):
with open(f'{name}', 'rb') as pickle_temp:
db = pickle.load(pickle_temp)
return db
make_db('test_db_two') I have also attatched the .py file is that helps.
Posts: 85
Threads: 12
Joined: May 2018
@ stanthaman42 Could you please explain with specifically what you are having problems with? ie what is your error code?
Posts: 11
Threads: 3
Joined: Jul 2018
(Aug-07-2018, 05:31 PM)Vysero Wrote: @stanthaman42 Could you please explain with specifically what you are having problems with? ie what is your error code?
I am not necessarily getting an error, when I try to open the dictionary file that I created it seems to be in a different language. I want to be able to take that dictionary file that I have with the data from the code to be able to look at it all at once in an excel file.
Posts: 85
Threads: 12
Joined: May 2018
Well in general if you want to convert a working python dictionary to a csv file I would suggest you use the 'csv' module, look here:
https://www.idiotinside.com/2015/04/14/e...in-python/
Posts: 11
Threads: 3
Joined: Jul 2018
(Aug-07-2018, 05:45 PM)Vysero Wrote: Well in general if you want to convert a working python dictionary to a csv file I would suggest you use the 'csv' module, look here:
https://www.idiotinside.com/2015/04/14/e...in-python/ I a confused on how I would implement my code to make this happen. I don't have a csv file right now? is there any way you could help me do this because I have been struggling with this for months and need to complete this today...
Thank you so much for you help.
Posts: 85
Threads: 12
Joined: May 2018
Aug-07-2018, 07:00 PM
(This post was last modified: Aug-07-2018, 07:00 PM by Vysero.)
(Aug-07-2018, 06:01 PM)stanthaman42 Wrote: need to complete this today...
Is this homework? Why are you just coming here today if its due today lol? Unfortunately, I am currently at work doing work related things and I dont have time to work this out.
Posts: 11
Threads: 3
Joined: Jul 2018
Aug-07-2018, 07:04 PM
(This post was last modified: Aug-07-2018, 07:04 PM by stanthaman42.)
(Aug-07-2018, 07:00 PM)Vysero Wrote: (Aug-07-2018, 06:01 PM)stanthaman42 Wrote: need to complete this today...
Is this homework? Why are you just coming here today if its due today lol? Unfortunately, I am currently at work doing work related things and I dont have time to work this out.
its a project that ive been working on for a while now. I have been on this forum before but was lead in the wrong direction. I would be more than appreciative to have your help after work. I don't want to take away from your job just looking for guidance. also, it is not due today I just need to fix my code asap because I still need to write a paper on this code. Thank you again
Posts: 85
Threads: 12
Joined: May 2018
(Aug-07-2018, 07:04 PM)stanthaman42 Wrote: its a project that ive been working on for a while now. I have been on this forum before but was lead in the wrong direction. I would be more than appreciative to have your help after work. I don't want to take away from your job just looking for guidance. also, it is not due today I just need to fix my code asap because I still need to write a paper on this code. Thank you again
Ah I see, well I was only wondering because they have a special section on python forums for homework. In any case I will take a closer look at it when I am off of work.
Posts: 11
Threads: 3
Joined: Jul 2018
Aug-07-2018, 08:46 PM
(This post was last modified: Aug-07-2018, 08:55 PM by nilamo.)
Hello Everyone,
I have been struggling with this assignment for quite sometime. I am attempting to take my code with the dictionary created but I need to take the dictionary data and place it into a .txt file or .csv file so I can use it in an excel document to further investigate the data created from the code. The code is as follows:
import pickle
def getAllCombinations(s, length):
if length == 0:
return ['']
else:
ret = []
for idx, c in enumerate(s):
combos = getAllCombinations(s[0:idx] + s[idx+1:], length - 1)
for i in range(len(combos)):
combos[i] = c + combos[i]
ret += combos
return ret
def getPermutations(s, ret, swapIdx = 0):
if swapIdx == len(s):
ret.append(int(''.join(s)))
for i in range(swapIdx, len(s)):
cpy = [c for c in s]
cpy[swapIdx], cpy[i] = cpy[i], cpy[swapIdx]
getPermutations(cpy, ret, swapIdx + 1)
def getAllPermutations(i):
s = str(i)
allPerms = set()
for i in range(len(s)):
curCombos = getAllCombinations(s, i + 1)
for combo in curCombos:
ret = []
getPermutations(combo, ret)
allPerms = allPerms.union(set(ret))
return list(allPerms)
def isPrime(n):
if n==2 or n==3: return True
if n%2==0 or n<2: return False
for i in range(3,int(n**0.5)+1,2): # only odd numbers
if n%i==0:
return False
return True
def getNumPrimes(i):
perms = getAllPermutations(i)
numprimes = 0
for perm in perms:
if (isPrime(perm)):
numprimes += 1
return numprimes
def find_maxPrimes():
max = 0
maxNum = 0
for i in range(1000, 10000):
cur = getNumPrimes(i)
if (max < cur):
max = cur
maxNum = i
return (maxNum)
def make_dict():
# max = 0
# maxNum = 0
#Printing here directly
Dict = {}
for i in range(1000, 10000):
Dict[i] = getNumPrimes(i)
return Dict
def make_db(name):
db = make_dict()
pickle_temp = open(f'{name}', 'wb')
pickle.dump(db,pickle_temp)
pickle_temp.close()
def open_db(name):
with open(f'{name}', 'rb') as pickle_temp:
db = pickle.load(pickle_temp)
return db
make_db('test_db_two') I will also attatch my .py file
I appreciate any help I can get as I need to complete this program as soon as possible so I can write a paper about the coding.
Posts: 85
Threads: 12
Joined: May 2018
Lets just try something simple at first here:
with open('your_file.txt', 'w') as data:
data.write(str(db))
|