Showing posts with label character count. Show all posts
Showing posts with label character count. Show all posts

Dictionary - Histogram - character frequency of a string

 # Dictionary - Histograms

# Create a histogram based on the frequency of characters in given string

# Histogram - String - Word Frequency


import operator


string1 = "An algorithm is a step-by-step procedure to solve a given problem. It is a well-defined computational procedure that takes some values as input, manipulates them using a set of instructions and produces some values as output and terminates in a finite amount of time. An algorithm, when formally written in a programming language is called a program, or code. Derivation of an algorithm that solves the problem and conversion of the algorithm into code, together,  is known as Algorithmic Problem Solving."


string1 = string1.lower()


ch = set(string1)


D = {c:string1.count(c) for c in ch if c.isalpha()}  


print("Histogram")

print("Length of dictionary = ", len(D))

print(D)


DSorted = sorted(D.items(), key=operator.itemgetter(1), reverse=True)


D_max3 = dict(DSorted[:3])

print("Most frequently used 3 characters:")

print(D_max3)


"""

Sample output

>python Dict_Hist.py

Histogram

Length of dictionary =  22

{'t': 37, 'a': 39, 'w': 4, 'l': 22, 'g': 14, 'h': 12, 'o': 38, 'u': 14, 's': 26, 'i': 32, 'm': 20, 'e': 40, 'd': 12, 'c': 10, 'r': 25, 'v': 8, 'n': 30, 'b': 4, 'y': 2, 'p': 14, 'k': 2, 'f': 7}

Most frequently used 3 characters:

{'e': 40, 'a': 39, 'o': 38}

"""

Java - File handling - line, word and character count


// File handling

/*
Write a Java program that displays the number of characters, lines and words in a text file.
*/

import java.io.*;

class FileLWC
{
     public static void main(String as[]) throws IOException
     {
         
          FileInputStream fin = new FileInputStream("ipt.txt");
          int lc = 0, wc = 0, cc = 0;
         
          while(true)
          {
              int i = fin.read();
              if(i==-1)
              {
                   lc++;
                   wc++;
                   break;
              }
              char c = (char)i;
             
              if(c=='\n' || c=='.')
              {
                   lc++;
                   wc++;
              }
              else if(c=='\t' || c==' ')
                   wc++;
              else if((c>='a' && c<='z')||(c>='A' && c<='Z'))
                   cc++;
                            
          }       
          System.out.println("Line count = "+lc);
          System.out.println("Word count = "+wc);
          System.out.println("Character count = "+cc);
          fin.close();
     }
}

File Handling 11: File line, word and character count

# Count number of characters, words and lines in a file
# File Count

fs = open("ipt.txt", "r")

charcount = 0
wordcount = 0
linecount = 0

all_lines = fs.readlines()
linecount = len(all_lines)

for Line in all_lines:
Line = Line.strip().lower()

tmp = Line.split(" ")
wordcount += len(tmp)

for ch in Line:
if(ch.isalpha()):
charcount += 1

print("Number of characters in file =", charcount)
print("Number of words in file =", wordcount)
print("Number of lines in file =", linecount)

fs.close()