Agree, especially there is an "Urgent" on the title. He must be too "urgent" to think about your answer. I will wonder if your effort will be in vain.
Best, Jiefei On Tue, Nov 5, 2019 at 4:52 PM Rolf Turner <[email protected]> wrote: > > 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 > homework.) > > cheers, > > 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 <[email protected]> > 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 > > ______________________________________________ > [email protected] mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ [email protected] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

