0

Hi all First time having to look for assistance but i am sort of at a brick wall for now. i have been learning python since August and i have been giving a challenge to complete for the end of Novemeber and i hope that there could be some help in making my code works. My task requires to find an ip address which occurs most frequent and count the number of times it appears also this information must be displayed to the user i have been giving 4 files .txt that have the ips. I am also required to make use of non trivial data structures and built in python sorting and/or searching functionalities, make use of functions, parameter passing and return values in the program. Below is a sample data structure they have recommended that i use: -

   `enter code here` 
    def analyse_logs(parameter):
    # Your Code Hear
    return something

    def extract_ip(parameter):
    # Your Code Hear
    return something

    def find_most_frequent(parameter):
    # Your Code Hear
    return something

    # Test Program
    def main():
    # Your Code Hear

    # Call Test Program
    main()

And below hear is what i have came up with and the code is completley differant from the sample that has been provided but what i have done dosnt give me output straight back instead creats a new text file which has been sorted but now what i am looking for: -

     enter code here

    def sorting(filename):
    infile = open(filename)
    ip_addr = []
    for line in infile:
    temp = line.split()
    for i in temp:
      ip_addr.append(i)
    infile.close()
    ip_addr.sort()
    outfile = open("result.txt", "w")
    for i in ip_addr:
    outfile.writelines(i)
    outfile.writelines(" ")
    outfile.close()
    sorting("sample_log_1.txt")e here

The code that i have created has sorted everything thats in the .txt file and outputs the most frequent that has been used all the way to the lest frequent. All i am look for is for an algorithim that can sort through the .txt file, find the IP address thats more frequent then print that ip out and how many times it appears. I hope i have provided everything and i am sure this is probally somthing very basic but i just cant get my head round it.

  • 1
    Please fix your indentation. And remove all the `enter code here` placeholders. – Barmar Nov 15 '21 at 23:52
  • Take a look at `collections.Counter()` – Barmar Nov 15 '21 at 23:53
  • thanks Barmar but am not sure if i am suppose to use collections.counter – lancer6098 Nov 16 '21 at 00:11
  • 1
    I thought that was allowed by "I am also required to make use of non trivial data structures and built in python sorting and/or searching functionalities" – Barmar Nov 16 '21 at 00:12
  • from the sample structure provided it looks to be they want us using 3 functions def analyse_logs(), def extract_ip () and def find_most_frequent() and print result – lancer6098 Nov 16 '21 at 00:19
  • Barmar if its of any help for you helping me out i can provide the full docume t that i was given. – lancer6098 Nov 16 '21 at 00:46
  • WHy do you think you can't use that function in one of those functions? – Barmar Nov 16 '21 at 00:47
  • I'm not sure how to help other than just writing the code for you. But the whole point of an exercise like this is to figure it out yourself. – Barmar Nov 16 '21 at 00:48
  • Thie biggest thing i require the help is coding to extract the ip that shows the most thats been a week i have been working away and i have hut the brick wall – lancer6098 Nov 16 '21 at 00:56
  • See https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – Barmar Nov 16 '21 at 00:58
  • Thanks again Barmar think i will just need to crack on and hope to find somthing that will come together – lancer6098 Nov 16 '21 at 01:06

1 Answers1

0

You should keep the number of times the IP addresses are repeated in a variable. You can use dictionary.

ip_count_dict = {"IP1": repeat_count, "IP2": repeat_count}

When first time you find a IP in your list set repeat_count 1 and after that if you find same ip again just increase counter.

For example,

ip_count_dict = {}
ip_list = ['1.1.1.1','1.1.1.2','1.1.1.3','1.1.1.1']
#Loop and count ips

#Final version of ip_count_dict {'1.1.1.1':2 , '1.1.1.2':1, '1.1.1.3':1}

With this dictionary you can store all ips and sort by their value.

P.S.: Dictionary keeps key,value pairs you can search "sort dictionary by value" after all counting thing done.

ozcnakd
  • 59
  • 2