Saturday 22 August 2020

Counting vowels in a string

 Using count() method:

string_data=input("Enter a string:")
vowels={'a','e','i','o','u'}
# Using Count() Method
list1=[]
for ch in string_data:
	if ch in vowels:
		if ch not in list1:
			list1.append(ch)
			print("{} occurred {} times".format(ch,string_data.count(ch)))

Without using count() method:

string_data=input("Enter a string:")
vowels={'a','e','i','o','u'}
d={}
for ch in string_data:
	if ch in vowels:
		d[ch]=d.get(ch,0)+1

print(d)
for key,value in sorted(d.items()):
	print(key,"occurred",value,"times")

Friday 21 August 2020

Counting number of occurrences of each character in a string


Using count() method:

string_data=input("Enter string: ")
l=[]
for x in string_data:
	if x not in l:
		l.append(x)
		print(x,"occurred",string_data.count(x),"times")

Without using count() method:

string_data=input("Enter string: ")
d={}
for x in string_data:
	d[x]=d.get(x,0) + 1
print(d)
print(d.items())
# for meaningful output
for key,value in sorted(d.items()):
	print(key,"occurred",value,"times")  

 

Question by Subscriber: 

 How to ignore special characters like ? , . etc

If you want to ignore special characters and focus on the alphabets and digits only, then you can do that by some extra work

You  can either import string class to get a set of all characters that you need to focus on, or you can manually add the set of characters you particularly want to focus on.

we will name them as valid_characters
Here goes the code:

import string
string_data=input("Enter string: ")
l=[]
valid_characters=set(string.ascii_letters+string.digits)
for x in string_data:
	if x in valid_characters:
		if x not in l:
			l.append(x)
			print(x,"occurred",string_data.count(x),"times")

Output: 

or

string_data=input("Enter string: ")
l=[]
valid_characters=set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+'0123456789')
for x in string_data:
	if x in valid_characters:
		if x not in l:
			l.append(x)
			print(x,"occurred",string_data.count(x),"times")



Hope you like it ..!

Thursday 13 August 2020

Removing duplicate characters from the string


 

 Sample Input: sssssssssssssssddddeeeeggggggghhhhh

Output: sdegh

1st way:

string_data=input("Enter string: ")
result=''
for character in string_data:
	if character not in result:
		result=result+character
print("You entered: ",string_data)
print("Final result after removing duplicate characters: \n", result )

  

2nd way:

string_data=input("Enter string: ")
list_data=[]

for character in string_data:
	if character not in list_data:
		list_data.append(character)
result="".join(list_data)		
print("You entered: ",string_data)
print("Final result after removing duplicate characters: \n", result )

Python Program for specific requirement-2

Sample Input: d8s2b5r6

Output:            dlsubgrx

string_data=input("Enter string: ")
result=''
for character in string_data:
	if character.isalpha():
		result=result+character
		previous=character
	else:
		digit=int(character)
		new_character=chr(ord(previous)+digit)
		result=result+new_character

print("You entered: ",string_data)
print("Final result : \n", result )

Python Program for specific requirements

Requirement:
Sample Input: s4b6c5a2d9

Output: ssssbbbbbbcccccaaddddddddd

string_data=input("Enter string: ")
result=''
for character in string_data:
	if character.isalpha():
		alphabet=character
	else:
		digit=int(character)
		result=result+alphabet*digit
print("You entered: ",string_data)
print("Final result: \n", result )

Requirement:
Sample Input:
ssssbbbbbbcccccaaddddddddd

Output: 4s6b5c2a9d    

string_data=input("Enter string: ")
result=''
previous=string_data[0]
character_count=1
i=1
while i<len(string_data):
	if string_data[i]==previous:
		character_count=character_count+1
	else:
		result=result+str(character_count)+previous
		previous=string_data[i]
		character_count=1
	if i==len(string_data)-1:
		result=result+str(character_count)+previous
	i=i+1
print("You entered: ",string_data)
print("Final result: \n", result )

Sort characters in a string

 Assume input string contains only alphabet symbols and digits.

We need to write a program to sort characters of the string were we will sort alphabets first and then digits will follow

Sample Input: C8N9A2U6R5B7

Output: ABCNRU256789

string_data=input("Enter some alphanumeric string to sort: ")
alphabets=[]
digits=[]
for ch in string_data:
	if ch.isalpha():
		alphabets.append(ch)
	else:
		digits.append(ch)
result=''.join(sorted(alphabets)+sorted(digits))
print("You entered: ",string_data)

print("Final result after Sorting: \n", result )

Merge two strings

 Merge two strings by taking characters alternatively from each string

Sample string 1 :  PYTHON
Sample string 2 :  python
Output:  PpYyTtHhOoNn 

Let us understand the two scenarios of this problem

Scenario1:  If both the strings are of same length:

string1=input("Enter first string: ")
string2=input("Enter second string: ")
i,j=0,0 # object initialization using Tuple Unpacking
result=""
while i<len(string1) or j<len(string2):
	result=result+string1[i]+string2[j]
	i=i+1
	j=j+1
print("You entered first string : ",string1)
print("You entered second string : ",string2)
print("Final result after alternate merge: \n", result )

Scenario 2:  If the strings have different length:

string1=input("Enter first string: ")
string2=input("Enter second string: ")
i,j=0,0 # object initialization using Tuple Unpacking
result=""
while i<len(string1) or j<len(string2):
	if i<len(string1):
		result=result+string1[i]
		i=i+1
	if j<len(string2):
		result=result+string2[j]
		j=j+1
print("You entered first string: ",string1)
print("You entered second string: ",string2)
print("Final result after alternate merge: \n", result )

Print characters present at even and odd index separately

 1st Way :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
string_data=input("Enter a string: ")
print("Characters present at even index position: ")
i=0
while i<len(string_data):
	print(string_data[i])
	i=i+2
print("Characters present at odd index position: ")
i=1
while i<len(string_data):
	print(string_data[i])
	i=i+2

 2nd Way:

string_data=input("Enter a string: ")
print("Characters present at even index position: ")
print(string_data[::2]) # or
print(string_data[0::2])
print("Characters present at odd index position: ")
print(string_data[1::2])

Reverse internal content of every second word

 Sample Input: one two three four five six seven eight

Output: one owt three ruof five xis seven thgie

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
string_data=input("Enter a string: ")
list1=string_data.split()
print(list1)
list2=[]
i=0
while i<len(list1):
	if i%2==0:
		list2.append(list1[i])
	else:
		list2.append(list1[i][::-1])# Reversing word and then adding to list2
	i=i+1
print(list2)
result=" ".join(list2)
print("The string you entered:", string_data, sep="\n")
print("The result after reversing order of words in the string: ", result, sep="\n")

Reverse the internal content of each word

 

 

 

Sample Input: Hello Python

Output: olleH nohtyP

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
string_data=input("Enter a string: ")
list1=string_data.split()
print(list1)
list2=[]
for word in list1:
	list2.append(word[::-1]) # Reversing word and then adding to list2
print(list2)
result=" ".join(list2)
print("The string you entered:", string_data, sep="\n")
print("The result after reversing order of words in the string: ", result, sep="\n")

 

Reverse order of words in a string

 

 

Sample Input: Hello everyone ! Welcome to MV's Code Guide

Output: Guide Code MV's to Welcome ! everyone Hello

Using Slice operator:

1
2
3
4
5
6
7
8
string_data=input("Enter a string: ")
list1=string_data.split()
#Using Slice Operator to reverse the items of list1
list2=list1[::-1]
# Using join() to form a string from items of list2 
result=" ".join(list2)
print("The string you entered:", string_data, sep="\n")
print("The result after reversing order of words in the string: ", result, sep="\n")

 Using While loop:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
string_data=input("Enter a string: ")
list1=string_data.split()
print(list1)
list2=[]
i=len(list1)-1
while i>=0:
	list2.append(list1[i])
	i=i-1
print(list2)
result=" ".join(list2)
print("The string you entered:", string_data, sep="\n")
print("The result after reversing order of words in the string: ", result, sep="\n")

Wednesday 12 August 2020

String Reversal

 Python program to reverse a string:

 

There are three ways to reverse a string in Python:

1st way: Using Slice Operator: 

(To learn about slice operator click here)

1
2
3
4
 
string_data=input("Enter data you want to reverse")
result=string_data[::-1]
print("The string data you entered is :", string_data)
print("The reversed string is:", result)
 

  2nd way: Using reversed() function:

Python provides inbuilt function reversed()

It returns reversed object and you can use join() method of strings to get your required output form reversed object.

1
2
3
4
5
6
7
string_data=input("Enter data you want to reverse")
reversed_object=reversed(string_data)
print("The string data you entered is :", string_data)
for character in reversed_object:
	print(character,end='')
print()
print(type(reversed_object))

3rd way : Using While loop:

1
2
3
4
5
6
7
8
string_data=input("Enter data you want to reverse: ")
result=""
i=len(string_data)-1
while i>=0:
	result=result+string_data[i]
	i=i-1
print("The string data you entered is :", string_data)
print("String data after reversal: ", result)



Saturday 30 May 2020

Adaptive Resonance Theory









Stability-Plasticity Dilemma




Counter Propogation




















Tuesday 26 May 2020

Application of ANN


Areas of Application

Followings are some of the areas, where ANN is being used. It suggests that ANN has an interdisciplinary approach in its development and applications.

Speech Recognition

Speech occupies a prominent role in human-human interaction. Therefore, it is natural for people to expect speech interfaces with computers. In the present era, for communication with machines, humans still need sophisticated languages which are difficult to learn and use. To ease this communication barrier, a simple solution could be, communication in a spoken language that is possible for the machine to understand.
Great progress has been made in this field, however, still such kinds of systems are facing the problem of limited vocabulary or grammar along with the issue of retraining of the system for different speakers in different conditions. ANN is playing a major role in this area. Following ANNs have been used for speech recognition −
  • Multilayer networks
  • Multilayer networks with recurrent connections
  • Kohonen self-organizing feature map
The most useful network for this is Kohonen Self-Organizing feature map, which has its input as short segments of the speech waveform. It will map the same kind of phonemes as the output array, called feature extraction technique. After extracting the features, with the help of some acoustic models as back-end processing, it will recognize the utterance.

Character Recognition

It is an interesting problem which falls under the general area of Pattern Recognition. Many neural networks have been developed for automatic recognition of handwritten characters, either letters or digits. Following are some ANNs which have been used for character recognition −
  • Multilayer neural networks such as Backpropagation neural networks.
  • Neocognitron
Though back-propagation neural networks have several hidden layers, the pattern of connection from one layer to the next is localized. Similarly, neocognitron also has several hidden layers and its training is done layer by layer for such kind of applications.

Signature Verification Application

Signatures are one of the most useful ways to authorize and authenticate a person in legal transactions. Signature verification technique is a non-vision based technique.
For this application, the first approach is to extract the feature or rather the geometrical feature set representing the signature. With these feature sets, we have to train the neural networks using an efficient neural network algorithm. This trained neural network will classify the signature as being genuine or forged under the verification stage.

Human Face Recognition

It is one of the biometric methods to identify the given face. It is a typical task because of the characterization of “non-face” images. However, if a neural network is well trained, then it can be divided into two classes namely images having faces and images that do not have faces.
First, all the input images must be preprocessed. Then, the dimensionality of that image must be reduced. And, at last it must be classified using neural network training algorithm. Following neural networks are used for training purposes with preprocessed image −
  • Fully-connected multilayer feed-forward neural network trained with the help of back-propagation algorithm.
  • For dimensionality reduction, Principal Component Analysis
is used.

Image Processing and Character recognition: Given ANNs ability to take in a lot of inputs, process them to infer hidden as well as complex, non-linear relationships, ANNs are playing a big role in image and character recognition. Character recognition like handwriting has lot of applications in fraud detection (e.g. bank fraud) and even national security assessments. Image recognition is an ever-growing field with widespread applications from facial recognition in social media, cancer detention in medicine to satellite imagery processing for agricultural and defense usage. The research on ANN now has paved the way for deep neural networks that forms the basis of “deep learning” and which has now opened up all the exciting and transformational innovations in computer vision, speech recognition, natural language processing — famous examples being self-driving cars.

 Forecasting: Forecasting is required extensively in everyday business decisions (e.g. sales, financial allocation between products, capacity utilization), in economic and monetary policy, in finance and stock market. More often, forecasting problems are complex, for example, predicting stock prices is a complex problem with a lot of underlying factors (some known, some unseen). Traditional forecasting models throw up limitations in terms of taking into account these complex, non-linear relationships. ANNs, applied in the right way, can provide robust alternative, given its ability to model and extract unseen features and relationships. Also, unlike these traditional models, ANN doesn’t impose any restriction on input and residual distributions.

Competitive Learning Neural Network