0% found this document useful (0 votes)
3 views9 pages

Toc 1-7

The document contains multiple programming tasks, each with Java code implementations. Tasks include counting 1s and 0s in a string, checking for three consecutive 1s, validating strings ending with '101', determining if a number is divisible by 2, performing tokenization using NLTK, and working with regular expressions. Additionally, it outlines the design of a PDA and a Turing Machine for specific language acceptance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Toc 1-7

The document contains multiple programming tasks, each with Java code implementations. Tasks include counting 1s and 0s in a string, checking for three consecutive 1s, validating strings ending with '101', determining if a number is divisible by 2, performing tokenization using NLTK, and working with regular expressions. Additionally, it outlines the design of a PDA and a Turing Machine for specific language acceptance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

TOC Pr - 1

Q.1. Design a program to create a machine which counts the number of 1s and 0s in the
string.
CODE:-
import java.util.Scanner;
import java.util.ArrayList;

public class Design {


String str;
char b2;
int len;
int comp = 0;
ArrayList<String> aL = new ArrayList<>();
ArrayList<String> bL = new ArrayList<>();

void initial() {
System.out.println("Input String of 1 and 0\n");
Scanner obj = new Scanner(System.in);
str = obj.nextLine();
len = str.length();
if (len > 0) {
for (int i = 0; i < str.length(); i++) {
char bl = str.charAt(i);
if (bl == '1' || bl == '0') {
// Valid characters, continue checking
} else {
System.out.println("Please provide valid string");
System.exit(0);
}
}
first();
}
}

void first() {
if (comp < len) {
char al = str.charAt(comp);
if (al == '1') {
comp++;
aL.add("A");
first();
} else {
comp++;
bL.add("B");
first();
}
} else {
System.out.println("Total count of 1 in the string: " + aL.size());
System.out.println("Total count of 0 in the string: " + bL.size());
}
}
public static void main(String[] args) {
Design dl = new Design();
dl.initial();
}
}

OUTPUT:-

TOC Pr - 2

Q.1. Design a program to create a machine to check whether a string has the consecutive
three ones.
CODE:-
import java.util.Scanner;
public class Design1{
String str;
int len;
int comp = 0;

void initial() {
System.out.println("Input strings of 0's and 1's");
Scanner sc = new Scanner(System.in);
str=sc.nextLine();
len=str.length();
if(len>0){
for(int i = 0; i<str.length(); i++) {
char bl = str.charAt(i);
if( bl !='1' && bl !='0') {
System.out.println("Please provide a valid input ");
System.exit(0);
}}
first();
}}
void first() {
checkPattern('1',this::second);
}
void second() {
checkPattern('1',this::third);
}
void third() {
checkPattern('1',this::last);
}

void last() {
if (comp<len ) {
comp++;
last();
} else {
System.out.println("String will be accepted");
System.exit(0);
}
}

void checkPattern (char expected, Runnable nextStep) {


if(comp<len) {
char al = str.charAt(comp);
if(al == expected) {
comp++;
nextStep.run();
} else {
comp++;
first();
}
} else {
System.out.println("Not a valid string");
}
}

public static void main(String [] args) {


Design1 dl = new Design1();
dl.initial();
}
}
​ TOC Pr - 3

Q.1. Design a program to accept the strings that ends with 101.
Code:-
import java.util.Scanner;
public class Design5 {
​ String str;
​ char br;
​ int len;
​ int comp = 0;
​ void initial()
​ {
​ ​ System.out.println("Input Strings of 0 and 1\n");
​ ​ Scanner obj = new Scanner(System.in);
​ ​ str = obj.nextLine();
​ ​ len = str.length();
​ ​ if(len>0)
​ ​ {
​ ​ ​ for(int i =0; i<str.length();i++) {
​ ​ ​ ​ char b1 = str.charAt(i);
​ ​ ​ ​ if(b1=='1' || b1=='0') {
​ ​ ​ ​ }
​ ​ ​ ​ else
​ ​ ​ ​ {
​ ​ ​ ​ ​ System.out.println("Please provide valid input");
​ ​ ​ ​ ​ System.exit(0);
​ ​ ​ ​ }
​ ​ ​ }
​ ​ ​ first();
​ ​ }
​ }
​ void first() {
​ ​ if(comp<len) {
​ ​ ​ char a1=str.charAt(comp);
​ ​ ​ if(a1=='1') {​ ​ ​
comp++;
​ ​ ​ ​ second();
​ ​ ​ } else {
​ ​ ​ ​ comp++;
​ ​ ​ ​ first();
​ ​ ​ }
​ ​ } else {
​ ​ ​ System.out.println("Not a valid string!");
​ ​ }
​ }
​ void second() ​{
​ ​ if(comp<len) {​
​ ​ ​ char a1 = str.charAt(comp);
​ ​ ​ if(a1=='0') {
​ ​ ​ ​ comp++;
​ ​ ​ ​ third();
​ ​ ​ } else {
​ ​ ​ ​ comp++;
​ ​ ​ ​ second();
​ ​ ​ }
​ ​ } else {
​ ​ ​ System.out.println("Not a valid String");
​ ​ }
​ }
​ void third() {
if(comp<len) {​
​ ​ ​ char a1 = str.charAt(comp);
​ ​ ​ if(a1=='1') {
​ ​ ​ ​ comp++;
​ ​ ​ ​ last();
​ ​ ​ } else {
​ ​ ​ ​ comp++;
​ ​ ​ ​ first();
​ ​ ​ }
​ ​ } else {
​ ​ ​ System.out.println("Not a valid String");
​ ​ }
​ }
​ void last() {
​ ​ if(comp<len) {
​ ​ ​ char a1 = str.charAt(comp);
​ ​ ​ if(a1=='1') {
​ ​ ​ ​ comp++;
​ ​ ​ ​ second();
​ ​ ​ } else {
​ ​ ​ ​ comp++;
​ ​ ​ ​ third();
​ ​ ​ }
​ ​ } else {
​ ​ ​ System.out.println("String will be accepted");
​ ​ ​ System.exit(0);
​ ​ }
​ }
​ public static void main(String [] args) {
​ ​ Design5 d1 = new Design5();
​ ​ d1.initial();
​ }
}

Output:-

TOC Pr - 04

Aim:- Design a program for accepting decimal numbers divisible by 2.

Code:-
import java.util.Scanner;
public class DiviSor {
​ public static void main(String[] args) {
​ ​ Scanner in = new Scanner(System.in);
​ ​ System.out.println("Enter Number:");
​ ​ int num = in.nextInt();

​ ​ if(num % 2 == 0)
​ ​ ​ System.out.println("Divisible by 2");
​ ​ else
​ ​ ​ System.out.println("Not divisible by 2");
​ ​
​ }
}

Output:-

TOC Pr - 05

Aim:- Write a program to perform tokenization of given input.


Code:-

import nltk
nltk.download()
sentence_data = "You're braver than you believe. You must be the change you wish to
see in the world"
nltk_tokens=nltk.sent_tokenize(sentence_data)
print(nltk_tokens)

Output:-

Word Tokenization:-

Code:-
import nltk
nltk.download()
sentence_data = "You're stronger than you believe. You must be the change you wish to
see in the world"
nltk_tokens=nltk.word_tokenize(sentence_data)
print(nltk_tokens)

Output:-
TOC Pr - 6

Aim:- Working with Regular Expressions.

1. re.findall() :- \d matches anu decimal digit; this is equivalent to the class[0-9]. +1 or more
occurrences of the pattern to its left.

Code:-
import re
string = "hello 09 hii 30.Howdy 34"
pattern = '\d+'
result = re.findall(pattern,string)
print(result)

Output:-

2. re.split()

Code:-
import re
string = "Twelve:12 Eighty nine:89"
pattern = '\d+'
result = re.split(pattern,string)
print(result)

Output:-

3. re.sub() :- The method returns a string where matched occurrences are replaced with the
code variable.

Code:-
import re
string = "abc 12\de 23\n f45 6"
pattern = '\s'
replace = ""
new_string = re.sub(pattern,replace,string)
print(new_string)

Output:-
4. re.search():- If the search is successful, re.search(). returns a match object; if not, it returns
None.

Code:-
import re
string = "Python is fun"
match = re.search('\APython',string)
if match:
print("Pattern found inside the string")
else:
print("Pattern not found")
Output:-

TOC Pr- 07

Aim:- Design a PDA to accept wcw^r where, w is any string and w^r is the reverse of the
string and c is the special symbol.

TOC Pr - 8

Aim:- Design a Turing Machine that accepts the following language

L = { a^n b^n | n>0 }

You might also like