Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
public static void main(String[] args) {
String str="4678912356012356";
System.out.println(getSequences(str));
}
static String getSequences(String str){
StringBuffer sb=new StringBuffer();
boolean flag=false;
for(int i=1;i<str.length();i++){
if(str.charAt(i-1)+1==str.charAt(i)){
sb.append(str.charAt(i-1));
flag=true;
}
else{
if(flag){
sb.append(str.charAt(i-1));
sb.append(';');
}
flag=false;
}
}
if(str.charAt(str.length()-2)+1==str.charAt(str.length()-1)){
sb.append(str.charAt(str.length()-1));
sb.append(';');
}
return sb.toString();
}
public class number_separation_epic {
public static void main(String [] args){
char [] str = {'4','6','7','8','9','1','2','3','5','6','0','1','2','3','5','6'};
char [] res = new char[str.length*2];
int prev = 0; int cur = 1; int i = 0;
while(cur < str.length){
int curval = (int)str[cur] - 48;
int preval = (int)str[prev] - 48;
if(curval == preval+1){
res[i] = str[prev];
prev += 1; cur += 1; i += 1;
}
else{
res[i] = str[prev]; res[i+1] = ';';
prev += 1; cur += 1; i += 2;
}
}
res[i] = str[prev]; res[i+1] = ';';
for(char c:res){
System.out.print(c);
}
}
}
#include "stdio.h"
#include "string.h"
int main(int argc, char** argv)
{
int j=0,i=0;
char str[] = "4678912356012356";
char str1[80];
char temp = str[0];
for(i=1;i<strlen(str);i++)
{
if(str[i] != temp+1)
{
printf("%c;",temp);
}else
{
printf("%c",temp);
}
temp = str[i];
}
i-=2;
if(temp != str[i]+1)
{
printf(";%c",temp);
}else
{
printf("%c",temp);
}
return 0;
}
we will just loop on the array, and we will have 3 conditions either:
* ar[i] = ar[i+1] -1 -> print ar[i]
* ar[i] != ar[i+1] -1 -> print ar[i] + ";"
* i is the last element in the array -> print ar[i] + ";"
Code:
for(int i=0;i<n;i++)
{
if(i == n-1)
cout << ar[i] << ";" << endl;
else if(ar[i] == ar[i+1] - 1)
cout << ar[i];
else
cout << ar[i] << ";";
}
#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;
}
#include <stdio.h>
int main(void) {
int i=0, diff=0;
char chr[40] = {[0 ... 39] = '\0'};
printf("Enter {0-9}* : ");
scanf("%s", chr);
while(chr[i] != '\0' && chr[i] >= '0' && chr[i] <= '9'){
if (chr[i+1] == '\0') {
printf("%c;\n", chr[i]);
break;
} else {
if (((chr[i] - '0') % 10) - ( (chr[i+1] - '0') % 10) == -1)
printf("%c",chr[i]);
else
printf("%c;",chr[i]);
}
++i;
}
return 0;
}
#include<iostream>
using namespace std;
void ProcessFunction(char a[]);
int main()
{
char a[100];
cin>>a;
ProcessFunction(a);
}
void ProcessFunction(char a[])
{
int i=0;
int j=i;
while(a[i]!='\0')
{
if(a[i]==a[i+1]-1)
i++;
else
{
for(int k=j;k<=i;k++)
{
cout<<a[k];
}
cout<<";";
i++;
j=i;
}
}
}
public void printStr(String str)
{
String s = String.valueOf(str.charAt(0));
for(int i =1; i< str.length();i++)
{
if(str.charAt(i)-str.charAt(i-1)==1)
s+=str.charAt(i);
else
{
if(s.length()>=2) System.out.print(s+";");
s=String.valueOf(str.charAt(i));
}
}
if(s.length()>=2) System.out.print(s+";");
}
public void printStr(String str)
{
String s = String.valueOf(str.charAt(0));
for(int i =1; i< str.length();i++)
{
if(str.charAt(i)-str.charAt(i-1)==1)
s+=str.charAt(i);
else
{
if(s.length()>=2) System.out.print(s+";");
s=String.valueOf(str.charAt(i));
}
}
if(s.length()>=2) System.out.print(s+";");
}
In C:
void printStr(char * cArr)
{
char s [128];
*s = cArr[0];
int shift =0;
for(int i=1;cArr[i]!='\0';i++)
{
if(cArr[i]-cArr[i-1]==1)
{*(s+(++shift)) = cArr[i];}
else
{
*(s+(++shift)) = '\0';
if(strlen(s)>=2) cout<<s<<";";
*s=cArr[i];
shift=0;
}
}
*(s+(++shift)) = '\0';
if(strlen(s)>=2) cout<<s<<";";
}
public static void main(String[] args){
String s="4678912356012356";
String str=separateSequance(s);
System.out.print(str);
}
private static String separateSequance(String s) {
// TODO Auto-generated method stub
boolean sign=true;
StringBuilder sb=new StringBuilder();
int index=0;
for(int i=0;i<s.length();i++){
if(i==s.length()-1&&sign==true)
sb.append(s.substring(index, s.length()));
int distance=1;
if(i!=0)
distance=s.charAt(i)-s.charAt(i-1);
if(distance!=1)
sign=false;
if(sign==false){
sb.append(s.substring(index,i)+";");
index=i;
sign=true;
}
}
return sb.toString();
}
//input: 4678912356012356
//output: 4;6789;123;56;0123;56;
#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>
int separate(char *,char *);
int main(){
char old[20]="4678912356012356";
printf("before: %s",old);
char new[30];
separate(old,new);
printf("\nafter: %s\n",new);
return 0;
}
void separate(char *old,char*new){
int i = 0;
int j = 0;
while(old[i]!='\0'){
new[j] = old[i];
if(old[i]+1!=old[i+1]){
new[++j]=';';
}
i++;
j++;
}
new[j]='\0';
return;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "4678912356012356";
string str = str1 + "-";
int j = 0;
int k = 0;
for(int i = 0; i < str.length() - 1; i++)
{
j = str[i];
if(j != (str[i+1] - 1))
{
cout<<str.substr(k,i-k+1)<<";";
k = i+1;
}
}
cout<<endl;
return 0;
}
public class PrintSequences {
public static void main(String[] args){
String input = "4678912356012356";
boolean x=false;
int j =1;
for(int i = 0; i<input.length()-1;i++)
{
if((input.charAt(j)-input.charAt(i))==1){
System.out.print(input.charAt(i));
x = true;
}
else if((input.charAt(j)-input.charAt(i))!=1 && x){
System.out.print(input.charAt(i)+";");
x=false;
}
if(j==input.length()-1&&(input.charAt(j)-input.charAt(i))==1)
System.out.print(input.charAt(j)+";");
j++;
}
}
}
int main(){
char *str = "4678912356012356";
int prev = -2;
int start=0;int j=0;int i=0;
for(i=0;i<strlen(str)+1;i++){
if(i!=0 && str[i]-'0' != prev + 1){
// printf("start = %i: i = %i\n" , start,i);
if(i- start >1){
for(j=start;j<i;j++){
printf("%c",str[j]);
}
printf(";");
}
start = i;
}
prev = str[i] - '0';
}
return 0;
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "4678912356012356";
string result = str.Substring(0, 1);
for (int i = 1; i < str.Length-1; i++)
{
if (str[i-1] + 1 == str[i])
{
result+=str.Substring(i,1);
}
else
{
result += ";"+str.Substring(i, 1);
}
}
Response.Write(result);
}
}
protected void Page_Load(object sender, EventArgs e)
{
string str = "4678912356012356";
string result = str.Substring(0, 1);
for (int i = 1; i < str.Length; i++)
{
if (str[i-1] + 1 == str[i])
{
result+=str.Substring(i,1);
}
else
{
result += ";"+str.Substring(i, 1);
}
}
Response.Write(result);
}
public class SequenceNumber {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String num =scan.nextLine();
StringBuffer sb = new StringBuffer();
for(int i=1; i<num.length(); i++){
if(num.charAt(i-1)+1 == num.charAt(i)){
sb.append(num.charAt(i-1));
}else{
sb.append(num.charAt(i-1));
sb.append(';');
}
}
sb.append(num.charAt(num.length()-1));
System.out.println(sb.toString());
}
}
private static string separateSequance(string s)
{
s = "2123443254546467876868991234";
//"4678912356012356";
char[] p = new char[s.Length];
p = s.ToCharArray();
string[] sc = new string[s.Length * 2];
int sign = 1;
sc.SetValue(p[0].ToString(), 0);
for (int i = 0; i < s.Length; i++)
{
if (Int32.Parse(sc[sign - 1]) + 1 == Int32.Parse(p[i].ToString()))
{
sc.SetValue(p[i].ToString(), sign);
sign = sign + 1;
}
else
{
if (sign == 1)
{
sc.SetValue(p[i].ToString(), sign - 1);
}
else if (sc[sign - 2].ToString() == ";")
{
sc.SetValue(p[i].ToString(), sign - 1);
}
else
{
if (sc[0].ToString() == p[0].ToString())
{
}
else
{
sc.SetValue(";", sign);
sc.SetValue(p[i].ToString(), sign + 1);
sign = sign + 2;
}
}
}
}
return s;
}
private static string separateSequance(string s)
{
s = "2123443254546467876868991234";
//"4678912356012356";
char[] p = new char[s.Length];
p = s.ToCharArray();
string[] sc = new string[s.Length * 2];
int sign = 1;
sc.SetValue(p[0].ToString(), 0);
for (int i = 0; i < s.Length; i++)
{
if (Int32.Parse(sc[sign - 1]) + 1 == Int32.Parse(p[i].ToString()))
{
sc.SetValue(p[i].ToString(), sign);
sign = sign + 1;
}
else
{
if (sign == 1)
{
sc.SetValue(p[i].ToString(), sign - 1);
}
else if (sc[sign - 2].ToString() == ";")
{
sc.SetValue(p[i].ToString(), sign - 1);
}
else
{
if (sc[0].ToString() == p[0].ToString())
{
}
else
{
sc.SetValue(";", sign);
sc.SetValue(p[i].ToString(), sign + 1);
sign = sign + 2;
}
}
}
}
return s;
}
public static void DisplayOnlySequence(string input)
{
char[] inputNumbers = input.ToCharArray();
StringBuilder sequenceString = new StringBuilder();
List<string> sequences = new List<string>();
Console.WriteLine("The Input String is : {0}", input);
Console.Write("The Sequences found in the string : ");
if (inputNumbers.Length > 1)
{
for (int i = 0; i < inputNumbers.Length; i++)
{
if (i == inputNumbers.Length - 1)
{
if ((inputNumbers[i] == inputNumbers[i - 1] + 1))
{
sequenceString.Append(inputNumbers[i] + ";");
sequences.Add(sequenceString.ToString());
}
}
else if (inputNumbers[i] == inputNumbers[i + 1] - 1)
{
sequenceString.Append(inputNumbers[i]);
}
else if (!String.IsNullOrEmpty(sequenceString.ToString()))
{
sequenceString.Append(inputNumbers[i] + ";");
sequences.Add(sequenceString.ToString());
sequenceString.Clear();
}
}
}
if(sequences.Count == 0)
{
Console.WriteLine("No Sequence Found in the Input String");
}
foreach (string str in sequences)
{
Console.Write(str);
}
Console.WriteLine();
}
#include<iostream.h>
class sequence
{
public:
void function(char *);
}
void sequence :: Function(char *seq)
{
int i=0;
while(seq[i]!='\0')
{
if(seq[i]==seq[i+1]-1)
{
cout<<seq[i];
i++;
}
else
{
cout<<seq[i];
cout<<";";
i++;
}
}
}
int main()
{
char *a;
cout<<"enter the integers without any space ";
cin>>a;
sequence seq;
seq.function(a);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterviewPractice
{
public class AllMyPrograms
{
public static void ArrangeSequsnce()
{
int[] nos = new int[] { 4, 3, 1, 2, 6, 7, 8, 9, 1, 2, 3, 5, 6, 0, 1, 2, 3, 5, 6 };
string[] result = new string[nos.Length * 2];
int current = 0, next = 0, expectednext = 0;
int resultcnt = 0;
for (int i = 0; i < nos.Length; i++)
{
current = nos[i];
next = nos[i + 1];
expectednext = current + 1;
if (next == expectednext)
{
result[resultcnt] = current.ToString();
}
else
{
result[resultcnt] = current.ToString();
result[resultcnt + 1] = ";";
resultcnt++;
}
resultcnt++;
}
}
}
}
static void Main(string[] args)
{
string sr = "4678912356012356";
List<char> lst = new List<char>(sr.ToArray());
StringBuilder sb = new StringBuilder();
if (lst.Count == 0) return;
sb.Append(lst[0]);
for (int i = 1; i < sr.Length; i++)
{
int j = i - 1;
if ((int)lst[i] - (int)lst[j] == 1)
{
sb.Append(lst[i]);
}
else
{
sb.Append(';');
sb.Append(lst[i]);
}
}
Console.Write(sb.ToString());
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class div_seq_semicolon
{
static void Main()
{
string seq = "467891235601235679";
seq_div(seq);
Console.ReadLine();
}
//------------------------------------------------
static void seq_div(string seq)
{
int index1=0, index2=1;
while(index2 < seq.Length)
{
if (seq[index2] != seq[index2 - 1] + 1)
{
subseq_print(seq, index1, index2-1);
Console.Write(", ");
index1 = index2;
index2++;
}
else {
index2++;
}
if (index2 == seq.Length)
{
subseq_print(seq, index1, index2 - 1);
}
}
}
//------------------------------------------------
static void subseq_print(string seq, int index1, int index2)
{
while (index1 <= index2)
{
Console.Write(seq[index1]);
index1++;
}
}
//------------------------------------------------
}
}
import java.util.Scanner;
public class seqs {
public static void main(String[] args) {
String in = "";
StringBuffer sb=new StringBuffer();
Scanner s=new Scanner(System.in);
System.out.println("input some numbers");
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();
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;
}
}
package Epic;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**************************************************************
* Write a program in c# or java or c
* print the sequences from the input given by the user
* separated by semicolon
* eg: 4678912356012356
*
* output: 6789;123;56;0123;56;
*
* example2: asbcdegjkxyza
*
* output:: bcde; jk; xyz;
*
* @author jhaxx030
*/
public class CommaSeparatedSequence {
/**
* @param args
*/
public static void main(String[] args) {
//int number =
/**
* TODO Check what does scanner do...?
*/
Scanner in = new Scanner(System.in);
System.out.println("Enter the digit you want to parse::");
String inputString = in.nextLine();
String str = printCommaSeparatedSequence(inputString);
System.out.println(str);
}
/**
*
* @param input
*/
static String printCommaSeparatedSequence(String inputString){
// Setting to previous character to dummy.
int previousChar='@';
// This represent the current Character.
char tempChar;
//This will hold one sequence at a time from the input.
List<Character> sequence = new ArrayList<Character>();
//This will hold the final string, comma-separated sequences.
String finalString = "";
for(int i=0 ; i <inputString.length(); i++){
// Getting the current character.
tempChar = inputString.charAt(i);
// Checking if the current char varies with previous one
// with only 1.
if(tempChar-previousChar == 1){
sequence.add(tempChar);
}else{
// If it varies more than 1, then end of sequence.
// So, add it to final string with comma.
finalString += addSequenceToString(sequence);
// Now define the new sequence.
sequence = new ArrayList<Character>();
// And add the current character.
sequence.add(tempChar);
}
// Set the previous char with current char.
previousChar = tempChar;
}
finalString += addSequenceToString(sequence);
return finalString;
}
/**
* Function for adding the sequence to string.
*
* @param sequence
* @return
*/
static String addSequenceToString(List<Character> sequence){
String sequenceString = "";
if(sequence.size() >1){
for(Character inputInt : sequence){
sequenceString += inputInt;
}
sequenceString += ";\t";
}
return sequenceString;
}
}
Python working code.
"""
6:13
@Python 2.7
write a program in c# or java or c
print the sequences from the input given by the user separated by semicolon
eg: 4678912356012356
output: 6789;123;56;0123;56;
- vamcrulz09 on February 19, 2012 in United States Report Duplicate | Flag
"""
class SemiSeq(object):
def __init__(self, inputs):
if inputs is None:
print 'Invalid Inputs'
raise SystemExit
self._inputs = str(inputs)
self._output = []
def __repr__(self):
return ';'.join(self._output)
def findSeq(self):
tmpOut = [self._inputs[0]]
lastNum = int(self._inputs[0])
i = 1
while i < len(self._inputs):
if int(self._inputs[i]) == lastNum + 1:
tmpOut.append(self._inputs[i])
lastNum = int(self._inputs[i])
i += 1
else:
if len(tmpOut) > 1:
self._output.append(''.join(tmpOut))
tmpOut = [self._inputs[i]]
lastNum = int(self._inputs[i])
i += 1
# terminate case special
if len(tmpOut) > 1:
self._output.append(''.join(tmpOut))
if __name__ == '__main__':
s = SemiSeq(4678912356012356)
s.findSeq()
print s
public static void printSemi(String s) {
int i = 0;
int j = 0;
StringBuffer sb = new StringBuffer();
for(i = 1; i < s.length(); i++) {
if(Integer.valueOf(s.charAt(i)) == Integer.valueOf(s.charAt(i - 1))+1) {
sb.append(s.charAt(i - 1));
j++;
}else {
if(j >= 1) {
sb.append(s.charAt(i - 1));
sb.append(";");
}
j = 0 ;
}
} sb.append(s.charAt(i - 1));
System.out.println(sb);
}
Here is a working code in C++
#include<cstdio>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
long long int arr[100], n, i, j, temp=0;
cin>>n;
for (i=0; i<100; i++)
{
arr[i]=-1;
}
i=0;
while (1)
{
if (n==0)
{
break;
}
arr[i]=fmod(n,10);
n=n/10;
i++;
}
if (i%2==0)
{
for (j=0; j<(i/2); j++)
{
temp=arr[j];
arr[j]=arr[i-j-1];
arr[i-j-1]=temp;
}
}
else
{
for (j=0; j<=(i/2); j++)
{
temp=arr[j];
arr[j]=arr[i-j-1];
arr[i-j-1]=temp;
}
}
for (j=0; j<i; j++)
{
cout<<arr[j];
if (arr[j+1]!=arr[j]+1)
{
cout<<";";
}
}
cout<<endl;
return 0;
}
public class Main {
public String genSequences(String input) {
StringBuffer sb = new StringBuffer();
int i;
for (i=0; i<input.length()-1; i++) {
sb.append(input.charAt(i));
if (input.charAt(i) != input.charAt(i+1) - 1) {
sb.append(";");
}
}
sb.append(input.charAt(i));
sb.append(";");
return sb.toString();
}
public static void main(String []args){
Main obj = new Main();
String input = new String("4678912356012356");
System.out.println(obj.genSequences(input));
}
one question: What happend to the leading '4' of the input? Or is it a typo?
- Suz February 19, 2012