₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,153 members, 8,420,587 topics. Date: Friday, 05 June 2026 at 05:16 AM

Toggle theme

Coding Challenge 5: Substring Generator - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingCoding Challenge 5: Substring Generator (4995 Views)

1 Reply (Go Down)

Coding Challenge 5: Substring Generator by Fayimora(op): 1:46pm On Jul 07, 2011
Implement using iteration recursion a substring generator that generates all the substrings of a string. For example, the substrings of the string "rum" are the seven strings

"r", "ru", "rum", "u", "um", "m", ""

Goodluck
Re: Coding Challenge 5: Substring Generator by GoodMuyis(m): 5:43pm On Jul 13, 2011
Correction sir cool
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 5:44pm On Jul 13, 2011
What correction?
Re: Coding Challenge 5: Substring Generator by GoodMuyis(m): 8:17pm On Jul 13, 2011

$myString = 'rum';
$myString2 = strrev($myString);

$myStrLen = strlen($myString);

for ($x = 0; $x < $myStrLen; $x++){

     echo substr($myString, $x). "\t\t";
     echo substr($myString2, $x). "\t\t";
}


Can this be usefull
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 8:49pm On Jul 13, 2011
Nope incorrect
Re: Coding Challenge 5: Substring Generator by tundebabzy: 2:11pm On Jul 15, 2011
with python

code:

def nairaland(word):
    #w = []-----------> redundant. I forgot to delete
    for x in range(len(word)):
        t = x
        for j in range(1,len(word[x:])+1):
            print word[t:t+j]
    print '""'



If you must have a recursion:


def nairaland_r(word, flag=1, w=None):
    #if not w: ---->redundant
     #   l = []
    #else:
     #   l = w
       
    if flag > len(word):
        print '""'
    else:
        for x in range(len(word)):
            print word[x:x+flag]
            if x+1 > len(word)-flag:
                break
        f = flag + 1

        nairaland_r(word,f,l)


Edit:

def nairaland(word):
    for x in range(len(word)):
        t = x
        for j in range(1,len(word[x:])+1):
            print word[t:t+j]
    print '""'



def nairaland_r(word, flag=1):
    if flag > len(word):
        print '""'
    else:
        for x in range(len(word)):
            print word[x:x+flag]
            if x+1 > len(word)-flag:
                break
        f = flag + 1

        nairaland_r(word,f)
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 2:40pm On Jul 15, 2011
Errrm unfortunaely i realy dont know python so what was ur result with the word 'rum' and 'fayimora'
Re: Coding Challenge 5: Substring Generator by tundebabzy: 9:05am On Jul 16, 2011
sorry, nairaland-bot refused to inform me that you had replied

no-recursion:
rum---------------> r ru rum u um m ""
fayimora---------> f fa fay fayi fayim fayimo fayimor fayimora a ay ayi ayim ayimo ayimor ayimora y yi yim yimo yimor yimora i im imo imor imora m mo mor mora o or ora r ra a ""

iterative recursion:
rum---------------> r u m ru um rum ""
fatimora---------> f a y i m o r a fa ay yi im mo or ra fay ayi yim imo mor ora fayi ayim yimo imor mora fayim ayimo yimor imora fayimo ayimor yimora fayimor ayimora fayimora ""

P.S i've edited my post to take care of some redundancies
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 8:23pm On Jul 16, 2011
cheesy Cool
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 8:56am On Aug 14, 2011
So just 1 person was able to solve this, wow!
Re: Coding Challenge 5: Substring Generator by Beaf: 11:03am On Aug 14, 2011
.
Re: Coding Challenge 5: Substring Generator by Beaf: 11:51am On Aug 14, 2011
I almost made a mock of myself rushing to post something I'd done in notepad on a PC without dev tools. Thank God I noticed the errors immediately I posted, otherwise man for no fit talk for programming section again! grin

Here goes (in C#):

    void GetAllSubstrings(string input, int startIndex)
    {
        int innerStartIndex = startIndex;
        string substring = input[innerStartIndex].ToString();
        Console.WriteLine(substring);
        while (innerStartIndex < input.Length)
        {
            innerStartIndex++;
            if (innerStartIndex == input.Length)
                break;
            substring += input[innerStartIndex];
            Console.WriteLine(substring);
        }
        startIndex++;
        if (startIndex < input.Length)
            GetAllSubstrings(input, startIndex);
    }
Re: Coding Challenge 5: Substring Generator by Mobinga: 10:40pm On Aug 14, 2011

[color=#00BB00]public[/color] [color=#00BB00]class[/color] arr {
[color=#00BB00]public[/color] [color=#00BB00]void[/color] mm(){
       String s = [color=#FF0000]"[/color][color=#FF0000]m[/color][color=#FF0000]o[/color][color=#FF0000]b[/color][color=#FF0000]i[/color][color=#FF0000]n[/color][color=#FF0000]g[/color][color=#FF0000]a[/color][color=#FF0000]"[/color];
       StringBuilder ss = [color=#FF9D00]new[/color] StringBuilder(s);
       String sss = ss.reverse().toString();
     [color=#0000EE]//   System.out.println(ss.reverse());
[/color]       rec(sss,s); }
  [color=#00BB00]public[/color] [color=#00BB00]void[/color] rec(String sss,String s ){
   [color=#FF9D00]for[/color]([color=#00BB00]int[/color] e = sss.length(); e>=[color=#FF0000]0[/color]; e--){
     StringBuilder lll = [color=#FF9D00]new[/color] StringBuilder(sss.substring(e));
     String ssss = lll.reverse().toString();
System.out.println(ssss);
}
       [color=#FF9D00]for[/color]([color=#00BB00]int[/color] z = [color=#FF0000]1[/color]; z<=s.length(); z++){
     StringBuilder lll = [color=#FF9D00]new[/color] StringBuilder(sss.substring(z));
     String ssss = lll.reverse().toString();
System.out.println(s.substring(z)); } }
[color=#00BB00]public[/color] [color=#00BB00]static[/color] [color=#00BB00]void[/color] main(String [] args){
arr psvm = [color=#FF9D00]new[/color] arr();
    psvm.mm();}}


Output ::

m
mo
mob
mobi
mobin
mobing
mobinga
obinga
binga
inga
nga
ga
a





-----------------
Non recursive -- //Would do that later.
-----------------
Re: Coding Challenge 5: Substring Generator by iGravity(m): 7:23pm On Aug 15, 2011
Why break your head on recursion when what you need is a substring generator?

In Ruby . . .

def substring_generator str
    1.upto(str.split('').size).each do |n|
        pp str.split('').combination(n).to_a
    end
end


Less clutter, more readable, gets the job done!
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 7:30pm On Aug 15, 2011
Thats why its a challenge! Its not an assignment so yeah Recursion tongue
Re: Coding Challenge 5: Substring Generator by iGravity(m): 7:39pm On Aug 15, 2011
Fayimora:
Thats why its a challenge! Its not an assignment so yeah Recursion tongue
this is the more reason to assume that this is some assignment and you're looking for the best solution. My opinion though -- it may not necessarily be right
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 7:50pm On Aug 15, 2011
Again its a challenge! AM not looking for the best solution but challenging you to recursion.
Re: Coding Challenge 5: Substring Generator by Beaf: 7:59pm On Aug 15, 2011
Fayimora:
Again its a challenge! AM not looking for the best solution but challenging you to recursion.
So far, my solution is the only recursive one that produces the expected result, you can test it.
Bros, abeg gif me di trophy na! Lol!
Re: Coding Challenge 5: Substring Generator by Fayimora(op): 8:03pm On Aug 15, 2011
Ok tell me How to run it. I know nothing about c#
Re: Coding Challenge 5: Substring Generator by Beaf: 10:41pm On Aug 15, 2011
Fayimora:
Ok tell me How to run it. I know nothing about c#
How about C++?
This is the same code in a simple C++ class:


#include <iostream>
#include <string>
using namespace std;

class SubstringGen
{
public:
void GetAllSubstrings(string input, int startIndex)
{
int innerStartIndex = startIndex;
string substring(1, input[innerStartIndex]);
cout << substring << endl;
while (innerStartIndex < input.length())
{
innerStartIndex++;
if (innerStartIndex == input.length())
break;
substring += input[innerStartIndex];
cout << substring << endl;
}
startIndex++;
if (startIndex < input.length())
GetAllSubstrings(input, startIndex);
};
};


You can call it like so:

SubstringGen substringGen;
substringGen.GetAllSubstrings("fayimora", 0);
1 Reply

Mini Web Application Coding Challenge For ProgrammersJava Coding Challenge: Task SchedulerCoding Challenge 3: Sum Of Primes234

Text-to-Speech For Nigerian LanguagesOnagoflyWeb Programming Tutorial In Python.