[R] [FORGED] Re: Help needed for one question (Urgent)

Rolf Turner r@turner @end|ng |rom @uck|@nd@@c@nz
Tue Nov 5 22:51:45 CET 2019

Richard:  I know that you mean well, but *please* don't do people's 
homework for them!!!  (They are *cheating* by asking R-help to do their 


Rolf Turner

On 6/11/19 4:27 AM, Richard O'Keefe wrote:
> This looks vaguely like something from exercism.
> Let's approach it logically.
>   xa xb xc ya yb zc
> We see two patterns here:
> A:  x x x y y z
> B: a b c a b c
> If only we had these two character vectors, we could use
>   paste(A, B, sep = "")
> to get the desired result.  So now we have reduced the
> problem to two simpler subproblems.  We have been given
> a clue that rep() might be useful.
> A: rep(c("x", "y", "z"), c(1, 2, 3))
> B: rep(c("a", "b", "c"), 3)
> But you were told not to use c().  So now we have three
> simpler subsubproblems:
> C: "x" "y" "z"
> D: 3 2 1
> E: "a" "b" "c"
> You were given another hint.  seq().  That builds a vector of numbers.
> Reading ?seq will give you
> D: seq(from = 3, to = 1, by = -1)
> or using ":" syntax,
> D: 3:1
> What about C and E?  This needs two more pieces of knowledge:
> - the variable letters,whose value is c("a","b",...,"y","z")
> - how vector indexing works in R.
> E: letters[1:3]
> C: letters[24:26]
> So now we can put all the pieces together:
> paste(rep(letters[24:26], 3:1), rep(letters[1:3], 2), sep = "")
> You were given
>   - seq
>   - rep
> as hints.  You were expected to look up string handling in R
> and find things like paste(), substr(), and nchar().
> What about the variable 'letters'?
> Well, you were expected to know or find out about substr.
> You were certainly expected to know about "vectorising".
> So you would naturally try substr("abc", 1:3, 1:3).
> And that would not work.
> So you would be expected to read the documentation:
> ?substr
> And then you would find that substr() *doesn't* do what
> you expect, but substring() *does*.  So
> C: substring("xyz", 1:3, 1:3)
> E: substring("abc", 1:3, 1:3)
> This is not really an exercise in R programming.
> In real R programming you *don't* avoid arbitrary aspects of the
> language and library, but use whatever is appropriate.
> So what *is* this exercise about?
> (1) It is an exercise in working backwards.  (See the classic book
> "How to Solve It" by Polya.)  You know what you must construct,
> you have been given some directions about what to use.  It's
> about saying "well, I could *finish* this task by doing this action,
> so what would I have to set up for that?"  In this case, the key
> step for me was seeing xa xb xc ya yb yc as (x,x,x,y,y,z)++(a,b,c,a,b,c).
> The mention of rep had me *looking* for repetitions like that.
> (2) It is an exercise in using the R documentation to figure out how to
> use rep and seq and what is available for splitting and pasting strings.
> There is of course no unique answer to this.
> substring("xaxbxcyaybzc", seq(from=1,to=11,by=2), seq(from=2,to=12,by=2))
> is another solution.  You didn't say you *had* to use rep.
> It's not the answer that matters for an exercise like this.
> It's how you get there.
> On Tue, 5 Nov 2019 at 23:40, Chandeep Kaur <chandeep.virdi using gmail.com> wrote:
>> Dear Team,
>> Could you please help me with the below question? How can I get the desired
>> output?
>> Produce the following sequence using only rep(), seq() and potentially
>> other functions/operators. You must not use c() nor explicit loops
>> “xa” “xb” “xc” “ya” “yb” “zc”
>> Thanks & Regards,
>> Chandeep Kaur

More information about the R-help mailing list