Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,743 members, 7,817,054 topics. Date: Saturday, 04 May 2024 at 01:18 AM

Coding Challenge 5: Substring Generator - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Coding Challenge 5: Substring Generator (4842 Views)

Mini Web Application Coding Challenge For Programmers / Java Coding Challenge: Task Scheduler / Coding Challenge 3: Sum Of Primes (2) (3) (4)

(1) (Reply) (Go Down)

Coding Challenge 5: Substring Generator by Fayimora(m): 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(m): 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(m): 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(m): 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(m): 8:23pm On Jul 16, 2011
cheesy Cool
Re: Coding Challenge 5: Substring Generator by Fayimora(m): 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

public class arr {
public void mm(){
       String s = "mobinga";
       StringBuilder ss = new StringBuilder(s);
       String sss = ss.reverse().toString();
     //   System.out.println(ss.reverse());
       rec(sss,s); }
  public void rec(String sss,String s ){
   for(int e = sss.length(); e>=0; e--){
     StringBuilder lll = new StringBuilder(sss.substring(e));
     String ssss = lll.reverse().toString();
System.out.println(ssss);
}
       for(int z = 1; z<=s.length(); z++){
     StringBuilder lll = new StringBuilder(sss.substring(z));
     String ssss = lll.reverse().toString();
System.out.println(s.substring(z)); } }
public static void main(String [] args){
arr psvm = new 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(m): 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(m): 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(m): 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)

How Do You Market Your Self As A Developer / Is Php Platform Independent? / I Want To Hire A Beginner Who Is Willing To Learn New Skills

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.