Search code examples
pythondictionaryasciiprimes

How can I Find the Largest Prime Value in a Dictionary of ASCII Sums?


I'm working on a Python script that reads a text file, splits it into a list of strings, and calculates the sum of ASCII values for each string. I've stored this data in a dictionary where the strings are the keys and the ASCII sums are the values. Now, I need help finding the largest prime value from this dictionary. Any suggestions or code examples would be greatly appreciated!

if __name__ == "__main":
    # Opening the file in read mode with all words in a txt file 
    my_file = open("english FILTERED.ALL.txt", "r") 

    # Reading the file 
    data = my_file.read() 

    # Replacing and splitting the text when newline ('\n') is seen. 
    data_into_list = data.split("\n") 
    print(data_into_list) 
    my_file.close() 

    # Create a dictionary to store the list values and the ASCII values with them
    result_dict = {}

    # Loop through the list and calculate the sum of ASCII values for each string
    for x in data_into_list:
        ascii_sum = sum(ord(char) for char in x)
        result_dict[x] = ascii_sum

    # Printing the original list
    print("The original list: " + str(data_into_list))

    # Printing the dictionary containing string-sum pairs
    print("String and their ASCII sum: " + str(result_dict))

I have successfully calculated the sum of ASCII values for each string and stored them in a dictionary. However, I'm now trying to find the largest prime value among these sums.

Here is currently an example of my current output

String and their ASCII sum : {'ACM': 209, 'ANSI': 299, 'ASAP': 293, 'ASCII': 361, 'Achilles': 805, 'Ada': 262, 'Afghanistan': 1124, 'Africa': 582, 'African': 692, 'Africans': 807, 'Airedale': 791, 'Alabama': 671, 'Alabamian': 886, 'Alaska': 589, 'Albania': 680, 'Albanian': 790, 'Albanians': 905, 'Alcibiades': 993, 'Alden': 484, 'Algeria': 693}

I'm seeking guidance on how to efficiently identify the largest prime value from a dictionary of ASCII sums, as I want to find the largest prime ASCII sum in my dictionary.


Solution

  • Is_prime function implemented to call. Iterating on the dictionary that u created, and it finds the largest prime number.

    Sample Code:

    def is_prime(n):
        if n <= 1:
            return False
        if n <= 3:
            return True
        if n % 2 == 0 or n % 3 == 0:
            return False
        i = 5
        while i * i <= n:
            if n % i == 0 or n % (i + 2) == 0:
                return False
            i += 6
        return True
    
    result_dict = {'ACM': 209, 'ANSI': 299, 'ASAP': 293, 'ASCII': 361, 'Achilles': 805, 'Ada': 262, 'Afghanistan': 1124, 'Africa': 582, 'African': 692, 'Africans': 807, 'Airedale': 791, 'Alabama': 671, 'Alabamian': 886, 'Alaska': 589, 'Albania': 680, 'Albanian': 790, 'Albanians': 905, 'Alcibiades': 993, 'Alden': 484, 'Algeria': 693} 
    
    largest_prime = None
    for string, ascii_sum in result_dict.items():
      if is_prime(ascii_sum):
        if largest_prime is None or ascii_sum > largest_prime:
          largest_prime = ascii_sum
    
    if largest_prime is not None:  
      largest_prime_strings = [string for string, ascii_sum in result_dict.items() if ascii_sum == largest_prime] 
      print("The largest prime:", largest_prime)
      print("Key:", largest_prime_strings)
    else:
      print("No prime values")
    

    Output:

    enter image description here