Epic Systems Interview Question
Country: United States
You have a problem here, I think. Have you ever tested the string like this "abcdz"? Your output is "abcd;". However, I do not think you should also print ";" here.
Algo :
1) check continuous chars , if they are consecutive , keep on printing them .
2) if some point the consecutive link breaks , start from the next element again . no need to go back because all the consecutive elements we have already checked & found some char which is not . So that char can't ever be considered in between a consecutive link ever .
This way we traverse the string just once & able to print the consecutive chars . So execution order is O(N).
Code for the above algo will be
void PrintCont( char * str){
int i = 0,c = 0,nextc = 0 ; char substr[100];
while(*str){
c = (int)(*str);
nextc = (int)(*(str + 1));
if(nextc && nextc - c == 1){
substr[i++] = *str;
}
elseif( i){
substr[i++] = *str;
substr[i] = '\0';
printf("%s\n" , substr);
i = 0;
}
str++;
}
}
static void printConsecutive(String str){
if(str==null || str.length()<=1)
return;
str = str.toLowerCase();
StringBuffer temp = new StringBuffer();
for(int i =0;i<str.length();i++){
if((temp.length()==0) || ((temp.length()>0) && (str.charAt(i) - temp.charAt(temp.length()-1) == 1)))
temp.append(str.charAt(i));
else{
if(temp.length()>1){
System.out.println(temp);
}
temp.delete(0, temp.length());
temp.append(str.charAt(i));
}
}
if(temp.length()>1){
System.out.println(temp);
}
}
char lower(char ch){
int diff = 'A' - 'a';
if(ch >= 'A' && ch <= 'Z')
return ch - diff;
else
return ch;
}
int main(int argc, char **argv){
char *orig = argv[1] ;
int i = 0 ;
bool flag = true;
while(orig[i] != '\0'){
if(i != 0){
if(lower(orig[i]) == lower(orig[i-1]) + 1){
if(flag){
printf("%c", orig[i-1]) ;
flag = false;
}
printf("%c", orig[i]) ;
}
else{
if(!flag){
printf(", ") ;
flag = true;
}
}
}
++i;
}
}
:)
public class Sequence {
public static void main(String args[])
{
String s="abcdekghjklmaaaahijkhdujdksdhkabcdref";
int j=0;
int u=0;
for(int i=0; i<(s.length()-1);i++)
{
j++;
char c=s.charAt(i);
char d=s.charAt(j);
if((int)(c+1)!=(int)d)
{
u=0;
continue;
}
{
if(u==0)
{
System.out.println();
System.out.print(s.charAt(i));
}
if(s.charAt(i)!=s.charAt(j-1))
{
System.out.print(s.charAt(i));
}
System.out.print(s.charAt(j));
u=1;
}
}
}
}
public static void continuousAlphabetic(char[] input){
int inLen = input.length();
char buff = input[0];
boolean patternFound = false;
for (int i = 1; i < inLen; ++i){
if (input[i] == buff + 1 || input[i] == buff + 1 - 32){
System.out.println(buff);
patternFound = true;
} else {
if (patternFound){
System.out.println(buff + ";");
}
patternFound = false;
}
buff = input[i];
}
}
Sorry I was wrong before, lack of one more condition in if...else
public static void continuousAlphabetic(char[] input){
int inLen = input.length;
char buff = input[0];
boolean patternFound = false;
for (int i = 1; i < inLen; ++i){
if (input[i] == buff + 1 || input[i] == buff + 1 - 32 || input[i] == buff + 1 + 32 ){
System.out.print(buff);
if (i==inLen-1){System.out.println(input[i] + ";");}
patternFound = true;
} else {
if (patternFound){
System.out.println(buff + ";");
}
patternFound = false;
}
buff = input[i];
}
}
Sorry I was wrong before, lack of one more condition in if...else
public static void continuousAlphabetic(char[] input){
int inLen = input.length;
char buff = input[0];
boolean patternFound = false;
for (int i = 1; i < inLen; ++i){
if (input[i] == buff + 1 || input[i] == buff + 1 - 32 || input[i] == buff + 1 + 32 ){
System.out.print(buff);
if (i==inLen-1){System.out.println(input[i] + ";");}
patternFound = true;
} else {
if (patternFound){
System.out.println(buff + ";");
}
patternFound = false;
}
buff = input[i];
}
}
Sorry I was wrong before, lack of one more condition in if...else
public static void continuousAlphabetic(char[] input){
int inLen = input.length;
char buff = input[0];
boolean patternFound = false;
for (int i = 1; i < inLen; ++i){
if (input[i] == buff + 1 || input[i] == buff + 1 - 32 || input[i] == buff + 1 + 32 ){
System.out.print(buff);
if (i==inLen-1){System.out.println(input[i] + ";");}
patternFound = true;
} else {
if (patternFound){
System.out.println(buff + ";");
}
patternFound = false;
}
buff = input[i];
}
}
This is written in python
The functional approach:
def fold(a, b):
if a[-1] == b[0]:
return a[:-1] + b
else:
return a + b
string = 'AbcDefljdflsjflmnopflsjflasjftuvWxYz'
string = string.lower()
reduce(fold,[x for x in izip(string, string[1:]) if ord(x[0])+1 == ord(x[1])])
The iterative approach :
def parse(string):
res = []
temp = ""
string = string.lower()
for i in xrange(len(string) - 1):
if (ord(string[i]) + 1) == ord(string[i+1]):
temp += string[i]
else:
temp += string[i]
if len(temp) > 1:
res.append(temp)
temp = ""
temp += string[-1]
if len(temp) > 1:
res.append(temp)
temp = ""
return res
string = 'AbcDefljdflsjflmnopflsjflasjftuvWxYz'
string = string.lower()
print parse(string)
#include <iostream>
#include <utility>
#include <string.h>
#include <map>
#include <conio.h>
using namespace std;
int main()
{
int j=0,i=0;
string a = "4678912356012356";
string temp[80];
int k=0;
for(i=0;i<16;i++,k++)
{
bool flag=false;
if(a[i]!=a[i+1]-1)
{
if(i>0)
{
temp[k]=a[i];
flag=true;
}
if(flag==true)
k++;
temp[k]=";";
}
else
{
temp[k]=a[i];
}
//k++;
}
int len=sizeof(temp)/sizeof(temp[0]);
for(k=0;k<len;k++)
cout<<temp[k];
getch();
return 0;
}
public void printString(String str)
{
str = str.toLowerCase();
StringBuffer s = new StringBuffer();
s.append(str.charAt(0));
for(int i =1; i< str.length();i++)
{
if(str.charAt(i)-str.charAt(i-1)==1)
s.append(str.charAt(i));
else
{
if(s.length()>=2) System.out.print(s+";");
s.delete(0, s.length());
}
}
if(s.length()>=2) System.out.print(s+";"); //print the last substring
}
The ouput should be abcdef;lmnop;tuvwxyz;
public static void main(String args[]){
String s= "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
ArrayList<String> arr=printConsecutive(s);
for(int i=0;i<arr.size();i++)
System.out.print(arr.get(i)+";");
}
private static ArrayList<String> printConsecutive(String s) {
ArrayList<String> array = new ArrayList<String>();
int beginIndex=0;
int diff='a'-'A';
for(int i=1;i<s.length();i++){
if((s.charAt(i)<='Z'&&s.charAt(i)>='A')||
(s.charAt(i)<='z'&&s.charAt(i)>='a')){
int gap =(s.charAt(i)<'a'?s.charAt(i)+diff:s.charAt(i))
-(s.charAt(i-1)<'a'?s.charAt(i-1)+diff:s.charAt(i-1));
if(i==1&&gap!=1)
beginIndex=2;
else if(gap!=1){
if(i-beginIndex!=1)
array.add(s.substring(beginIndex,i).toLowerCase());
beginIndex=i;
}
if(i==s.length()-1&&i!=beginIndex)
array.add(s.substring(beginIndex,i+1).toLowerCase());
}
}
return array;
}
I think the correct output should be "abcdef;lmnop;tuvwxyz;", it must be a typo... XD
public static void contdLetters(String input){
input = input.toLowerCase();
char buf = ';';
for (int i=0; i<input.length()-1; ++i){
if (input.charAt(i+1) != input.charAt(i)+1){
if (buf != ';'){
System.out.print(input.charAt(i));
System.out.print(";");
buf = ';';
}
} else {
System.out.print(input.charAt(i));
buf = input.charAt(i);
}
}
System.out.println(input.charAt(input.length()-1)+";");
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
public void semicolonSeparatedSequenes(String input){
int i = 0,first;
char next;
System.out.print(input.charAt(i));
while( i < input.length()-1){
//first = Integer.parseInt(Character.toString(input.charAt(i)));
first = (input.charAt(i));
//next = Integer.parseInt(Character.toString(input.charAt(i+1)));
next = (input.charAt(i+1));
if(next == first+1)
System.out.print(next);
else
System.out.print(";" + next);
i++;
}
}
public void semicolonSeparatedSequenes(String input){
int i = 0,first;
char next;
System.out.print(input.charAt(i));
while( i < input.length()-1){
//first = Integer.parseInt(Character.toString(input.charAt(i)));
first = (input.charAt(i));
//next = Integer.parseInt(Character.toString(input.charAt(i+1)));
next = (input.charAt(i+1));
if(next == first+1)
System.out.print(next);
else
System.out.print(";" + next);
i++;
}
}
import java.util.Scanner;
public class SemiColonPettern {
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the string");
String string=s.next();
StringBuffer buffer=new StringBuffer();
char c='a';
for (int i = 1; i < string.length(); i++) {
if(i==1)
buffer.append(string.charAt(0));
if(buffer.charAt(buffer.length()-1)==';')
{
buffer.append(string.charAt(i-1));
}
if(string.charAt(i-1)==string.charAt(i)-1)
{
buffer.append(string.charAt(i));
}
else
{
buffer.append(";");
}
}
System.out.println(buffer);
}
}
public class PrintAlphabetsequence
{
static void print(String s)
{
int start =0 ,end = 0;
char cur;
s = s.toLowerCase();
char prev =s.charAt(0);
for(int i=1; i< s.length();i++)
{
cur = s.charAt(i) ;
if(cur == prev + 1)
{
end = i ;
prev = cur;
}
else
{ if( start != end)
System.out.print(s.substring(start, end)+";");
start = end = i;
prev = cur;
}
}
if(start != end)
{
System.out.print(s.substring(start, end)+";");
}
}
public static void main(String[] args)
{
print("AbcDefljdflsjflmnopflsjflasjftuvWxYz");
}
}
import java.util.Scanner;
public class charSeqs {
public static void main(String[] args) {
String in = "";
StringBuffer sb=new StringBuffer();
@SuppressWarnings("resource")
Scanner s=new Scanner(System.in);
System.out.println("input your string");
while(!in.equals("exit")) {
if(s.hasNext()) {
sb.append(in); in = s.next();
System.out.println(in);
}
}
System.out.println(sb);
String strsb=sb.toString();
strsb=strsb.toLowerCase();
System.out.println(getSequences(strsb));
}
private static StringBuffer getSequences(String str) {
char[] cha=str.toCharArray();
StringBuffer sb=new StringBuffer();
boolean[] flg=new boolean[cha.length];
for(int i=0;i<cha.length-1;i++){
if(cha[i]+1==cha[i+1]){
flg[i+1]=true;
sb.append(cha[i]);
}else{
if(flg[i]==true){
sb.append(cha[i]);
sb.append(";");
}
}
}
if(flg[cha.length-1]==true){
sb.append(cha[cha.length-1]);
}
return sb;
}
}
Python working code.
Output of the OP's sample missing a 'l', should be:
abcdef; lmnop; tuvwxyz
"""
12:10
@Python 2.7
Print continuous alphabets from a sequence of arbitrary alphabets
For example:
Input: abcdefljdflsjflmnopflsjflasjftuvwxyz
Output: abcdef; mnop; tuvwxyz
Input: AbcDefljdflsjflmnopflsjflasjftuvWxYz
Output: abcdef; mnop; tuvwxyz
- Anony on February 02, 2012 in United States Report Duplicate | Flag
"""
class CountiA(object):
def __init__(self, inputs):
if inputs is None:
print 'invalid inputs'
raise SystemExit
self._inputs = inputs
def findSeq(self):
output = []
tmp = [self._inputs[0].lower()]
lastChar = self._inputs[0].lower()
for i in range(1, len(self._inputs)):
if ord(self._inputs[i].lower()) - ord(lastChar) == 1:
lastChar = self._inputs[i].lower()
tmp.append(self._inputs[i].lower())
else:
if len(tmp) > 1:
output.append(''.join(tmp))
tmp = [self._inputs[i].lower()]
lastChar = self._inputs[i].lower()
if len(tmp) > 1:
output.append(''.join(tmp))
return '; '.join(output)
if __name__ == '__main__':
ca = CountiA('abcdefljdflsjflmnopflsjflasjftuvwxyz')
print ca.findSeq()
ca = CountiA('AbcDefljdflsjflmnopflsjflasjftuvWxYz')
print ca.findSeq()
I think they have missed 'l' from the sequence "mnop" in the output. Here is a workin gcode in C++ considering the upper bound of the number of characters to be 100. we can make it dynamic is we wish.
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
void pp(char arr[], int st, int ed);
void pp(char arr[], int st, int ed)
{
int i;
for (i=st; i<ed; i++)
{
cout<<arr[i];
}
}
int main()
{
char arr[100];
int i, st, ct=0;
cin>>arr;
for (i=0; i<strlen(arr); i++)
{
arr[i]=tolower(arr[i]);
}
st=0;
i=1;
ct=1;
while (i<strlen(arr))
{
if (arr[i]!=arr[i-1]+1)
{
if (ct!=1)
{
pp(arr, st, i);
cout<<"; ";
}
st=i;
ct=1;
}
else
{
ct++;
}
i++;
}
pp(arr, st, i);
return 0;
}
public static List<String> epic(String s) {
List<String> r = new ArrayList<String>();
int left = 0, right = 0;
while (right < s.length()) {
while (right < s.length() - 1 && s.charAt(right) + 1 == s.charAt(right + 1))
right++;
if (right - left >= 1) {
r.add(s.substring(left,right+1));
}
left = right + 1;
right = left;
}
return r;
}
import java.util.*;
class charseq{
public static void main(String[] args){
String input ="AbcDefljdflsjflmnopflsjflasjftuvWxYz";
input = input.toLowerCase();
ArrayList arr = new ArrayList();
for(int i=0;i<input.length();i++){
arr.add(input.charAt(i));
}
Collections.sort(arr);
char prev = (char)arr.get(0);
char newchar ='-';
String str =""+prev;
String prevstr ="";
for(int i=1;i<arr.size();i++){
newchar=(char)arr.get(i);
if(prev == newchar) continue;
if(prev+1 == newchar){
str += newchar;
}
else{
if(prevstr != str){
str = str+";";
prevstr = str;
}
}
prev = newchar;
}
System.out.println(str);
}
}
please let me know if anything wrong with it im getting correct op
import java.util.*;
class charseq{
public static void main(String[] args){
String input ="AbcDefljdflsjflmnopflsjflasjftuvWxYz";
input = input.toLowerCase();
ArrayList arr = new ArrayList();
for(int i=0;i<input.length();i++){
arr.add(input.charAt(i));
}
Collections.sort(arr);
char prev = (char)arr.get(0);
char newchar ='-';
String str =""+prev;
String prevstr ="";
for(int i=1;i<arr.size();i++){
newchar=(char)arr.get(i);
if(prev == newchar) continue;
if(prev+1 == newchar){
str += newchar;
}
else{
if(prevstr != str){
str = str+";";
prevstr = str;
}
}
prev = newchar;
}
System.out.println(str);
}
}
public class continousAlphabets{
public static void main(String args[])
{
String str = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
str = str.toLowerCase();
int i = 0;
String temp = "";
while(i<str.length()-1){
if(str.charAt(i+1) == str.charAt(i) + 1)
{
temp = temp + str.charAt(i);
i++;
}
else
{
temp = temp + str.charAt(i);
if(temp.length() >=2){
System.out.println(temp + ";");
}
temp = "";
i++;
continue;
}
}
if(temp.charAt(temp.length() -1) + 1 == str.charAt(str.length()-1)){
temp = temp + str.charAt(str.length()-1);
}
System.out.println(temp);
}
}
If we add an invalid char by the end of the string, we don't need to worry about the last char. Given we are studying alphabet, so I add a space to the end, and the space won't continuous with any char.
package EPIC;
public class ContinuousAlphabets {
public static void printContiAlpha(String in){
String S = in.toLowerCase();
S = S + ' ';
char curr;
char next;
boolean continuous = false;
for (int i = 0; i <= S.length() - 2; i++) {
curr = S.charAt(i);
next = S.charAt(i + 1);
if(curr + 1 == next){
System.out.print(curr);
continuous = true;
}else if( continuous ){
System.out.print(curr + "; ");
continuous = false;
}
}
}
public static void main(String[] args) {
String in = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
printContiAlpha(in);
}
}
#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}
public void semicolonSeparatedSequenes(String input){
int i = 0,first;
char next;
System.out.print(input.charAt(i));
while( i < input.length()-1){
first = (input.charAt(i));
next = (input.charAt(i+1));
if(next == first+1)
System.out.print(next);
else
System.out.print(";" + next);
i++;
}
}
Perfect running code
Please let me know if you find any mistake or I haven't handled any case.
- thepraveen0207 October 25, 2012