Python Forum
Help Editing/Problem with outputs
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Editing/Problem with outputs
#1
Hello there. I am Currently working on a program that produces every number (1000-9999) and their prime permutations(single, double, triple, and quadruple digit numbers). When I print my output, it is telling me that 1000 has 2 prime permutations which is incorrect. Can anyone see if I made a mistake with my coding that might be easy to fix or what could be the cause of the wrong data?

Thanks, the code is posted below:


import math

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):
    for i in range(2, math.ceil(n**(1/2)) + 1):
        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

print(find_maxPrimes())

def find_maxPrimes2():
#    max = 0
#    maxNum = 0
#Printing here directly
    for i in range (1000, 10000):
        print(i, " ---> ", getNumPrimes(i))       
find_maxPrimes2()
Output:
1000 ---> 2 1001 ---> 4 1002 ---> 2 1003 ---> 7 1004 ---> 5 1005 ---> 3 1006 ---> 4 1007 ---> 8 1008 ---> 2 1009 ---> 6 1010 ---> 4
Reply


Messages In This Thread
Help Editing/Problem with outputs - by stanthaman42 - Jul-10-2018, 09:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  format json outputs ! evilcode1 3 1,758 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 636 Aug-23-2023, 08:52 AM
Last Post: perfringo
  I have written a program that outputs data based on GPS signal kalle 1 1,180 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,390 Jan-04-2022, 07:08 PM
Last Post: ndc85430
Question Problem editing images ielcraft1 2 1,754 Dec-16-2021, 06:48 PM
Last Post: BashBedlam
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,487 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,688 Mar-15-2021, 02:43 PM
Last Post: rybina
  ONE input => THREE outputs Tricia279 6 2,651 Jan-14-2021, 08:52 AM
Last Post: perfringo
  Multi set string inputs/outputs kwmcgreal 2 2,086 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 2,230 Sep-19-2020, 05:46 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020