Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Suppose you are given two strings like

String 1 = "ipsum"
String 2 = "Lorem ipsum is simply dummy text of the printing and typesetting industry. Lorem sipum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem pisum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem muips."

Challenge: Now you are suppose to search for the all the possible permutations of String 1 in String 2. You have to write the following program without using any string functions throughout the program.

The output of the program should print the possible permutation present in String 2.
Output for the following example:

ipsum sipum pisum muips

share|improve this question
1  
What about aaaa in aaaab, should it find it 24 times or just once? – mniip May 13 at 10:14
And what if the language has immutable strings, i.e you can't manipulate with strings in any way without some kind of string functions – mniip May 13 at 10:16
@mniip - in case of that it should say 24 times. To make the program more complex i guess printing it just once would be tougher. – iViki May 13 at 10:20
well then obviously, you shouldn't code in that particular language. – iViki May 13 at 10:23
3  
What is the objective primary winning criterion? – Howard May 13 at 12:59
show 4 more comments

3 Answers

Python 2.x - 79 77

I'm not exactly sure what you mean by "without using any string functions throughout the program."

from itertools import*
print[i for i in map(''.join,permutations(s))if i in S]
share|improve this answer
Shouldn't you have a 'print' there? – Inbar Rose May 13 at 12:41
Ah, yes I should. Adding... – Haidro May 13 at 12:50
Isn't print a string function? – Johannes Kuhn May 13 at 13:06
oh-oh-oh, ''.join, totally a string function – mniip May 13 at 13:08
Absolutely, and permutations(s) works on a string? – Johannes Kuhn May 13 at 13:14

Python 3

Effective algorithm

from collections import Counter

n1 = len(string1)
n2 = len(string2)
target = Counter(string1)
tlen = len(target)
test = Counter(string2[:n1])
eq = sum(test[c] == target[c] for c in target)

for i in range(n2 - n1):
    if eq == tlen: print(repr(string2[i:i+n1]), 'at offset', i)
    c = string2[i]
    if test[c] == target[c]: eq -= 1
    test[c] -= 1
    c = string2[i+n1]
    test[c] += 1
    if test[c] == target[c]: eq += 1
if eq == tlen: print(repr(string2[-n1:]), 'at offset', n2-n1)

Uses only length function for looping and substring operation for printing.

And output:

'ipsum' at offset 6
'sipum' at offset 81
'pisum' at offset 452
'muips' at offset 568
share|improve this answer

GoRuby 2.1

32 chars

n.ch.pe.e{|x|s x.j if h.id(x.j)}

assumes string to search for in variable n (needle) and string to search in in variable h (haystack). Ungolfed version:

needle.chars.permutation.each do |x|
  puts x.join if haystack.index(x.join)
end
share|improve this answer
I believe .index is a string function. – Jan Dvorak May 31 at 22:35
Why do you believe that? I am absolutely certain that it works with arrays: [4,5,6].index(6) #=> 2. See ruby-doc.org/core-2.0/Array.html#method-i-index – padde May 31 at 23:13
Sure it does, but haystack looks like a string to me... – Jan Dvorak May 31 at 23:16
Ah now i get your comment, sorry for the misunderstanding. And yes, you're right i'll have to come up with something else. I guess chars technically is a String function then, too. – padde May 31 at 23:20
But in the end even accessing a character can be viewed as a string function which makes this challenge impossible to solve. – padde May 31 at 23:21
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.