Skip to content

Commit

Permalink
Added run time inputs for Naive String Matching and Pigeonhole Sort (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
singh-shreya6 authored and jainaman224 committed May 19, 2019
1 parent 54e9b5a commit 78598d0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ void search(string text, string pattern)
{
int lengthText = text.length();
int lengthPattern = pattern.length();
int j;

for(int i = 0; i <= lengthText - lengthPattern; i++)
{
int j;

for(j = 0; j < lengthPattern; j++)
if(text[i + j] != pattern[j])
break;
Expand All @@ -22,16 +21,22 @@ void search(string text, string pattern)

int main()
{
string text = "namanchamanbomanamansanam";
string pattern = "aman";
search(text, pattern);
return 0;
string text, pattern;
cout << "Enter text" << endl;
cin >> text;
cout << "Enter pattern" << endl;
cin >> pattern;
search(text, pattern);
return 0;
}

/* Output
/*
Input:
text = "namanchamanbomanamansanam"
pattern = "aman"
Output:
Pattern found at 2
Pattern found at 8
Pattern found at 17
*/
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Main
import java.util.Scanner;

class Naive_String_Matching
{
public static void search(String text, String pattern)
{
Expand All @@ -20,16 +22,23 @@ public static void search(String text, String pattern)

public static void main(String[] args)
{
String text = "namanchamanbomanamansanam";
String pattern = "aman";
search(text, pattern);
Scanner sc = new Scanner(System.in);
System.out.println("Enter text");
String text = sc.nextLine();
System.out.println("Enter pattern");
String pattern = sc.nextLine();
Naive_String_Matching obj = new Naive_String_Matching();
obj.search(text, pattern);
}
}

/* Output
/*
Input:
text = "namanchamanbomanamansanam"
pattern = "aman"
Output:
Pattern found at 2
Pattern found at 8
Pattern found at 17
*/
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ def search(text, pattern):
if j == lengthPattern:
print("Pattern found at " + str(i + 1))

text = "namanchamanbomanamansanam"
pattern = "aman"
text = input()
pattern = input()
search(text, pattern)

'''
Input:
text = "namanchamanbomanamansanam"
pattern = "aman"
''' Output
Output:
Pattern found at 2
Pattern found at 8
Pattern found at 17
'''
39 changes: 24 additions & 15 deletions Pigeonhole_Sort/Pigeonhole_Sort.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public class PigeonholeSort {
import java.util.Scanner;

class Pigeonhole_Sort {
// Method for getting the maximum value
public static int getMax(int[] inputArray) {
int maxValue = inputArray[0];
Expand Down Expand Up @@ -39,19 +41,26 @@ public static void sort(int[] input) {
input[index++] = j + min_no;
}

public static void main(String[] args) {
System.out.println("Hello World");
int[] test = {
1,
4,
32,
2
};

sort(test);
for (int i = 0; i < test.length; i++)
System.out.print(test[i] + " ");
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
int n = sc.nextInt();
int[] array = new int[n];
System.out.println("Enter elements of array");
for(int i = 0; i < n; i++)
array[i] = sc.nextInt();
sort(array);
for(int i = 0; i < n; i++)
System.out.println(array[i] + " ");
}
}
//input = 1,4,32,2
// output = 1 2 4 32

/*
Input:
n = 4
array = [1, 4, 32, 2]
Output
array = [1, 2, 4, 32]
*/
29 changes: 19 additions & 10 deletions Pigeonhole_Sort/Pigeonhole_Sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ def pigeon_hole(input):
size = max_no - min_no + 1

# create holes
holes = [0]*size
holes = [0] * size
# populate holes
for i in input:
holes[i- min_no]+=1
holes[i- min_no] += 1

# sort
index = 0
for i in range(size):
while holes[i]>0:
holes[i] -=1
while holes[i] > 0:
holes[i] -= 1
input[index] = i + min_no
index+=1
index = index + 1
return input

test = pigeon_hole([2,3,5,4])
print(test)

#input = [2,3,5,4]
#output = [2,3,4,5]
print("Enter number of elements")
n = int(input())
print("Enter elements of array")
array = [int(x) for x in input().split(' ')]
pigeon_hole(array)
print(array)

'''
Input:
array = [1, 4, 32, 2]
Output
array = [1, 2, 4, 32]
'''

0 comments on commit 78598d0

Please sign in to comment.