• 0

Need Recheck and Help for Comp Sci assignment


Question

So basically I have these multiple questions that were given by my professor and I done several of it but I want to make sure whether these are corrects or not so please kindly help and check if any wrong

 

1. Which of the following strings would match the pattern: Pattern pattern = Pattern.compile("([Mm]e|[Yy}ou).*\\1$");

a. "Is it me or you"

b. "You're just You"

c. "Some come to me"

d. "Are you here for you or me"

e. "Me for you, and you for me"

f. "It's always me, me, me"

 

My answers: b, c, e, f

 

I posted a picture of my work 

2. Answer : C

3. Answer: A

Part B is shown on the link

For Part C please help me because it's unfinished and I do not think i get it right either

 

http://imgur.com/M4g9TBd,tRRpDLH,jd1j38y

 

 

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Are there any typos in this?

 

Pattern pattern = Pattern.compile("([Mm]e|[Yy}ou).*\\1$");

 

At the very least, that should be:

 

Pattern pattern = Pattern.compile("([Mm]e|[Yy]ou).*\\1$");

 

And your code... it looks like it says:

 

while(alist.isEmpty)

 

And that should probably say something like:

 

while(!alist.isEmpty())

Link to comment
Share on other sites

  • 0

ermm no i type everything from the assignment without changing.

 

As correctly pointed out by rfirth, the '}' character in the pattern you quoted should be a ']'. If as you imply the mistake is in source question you copied from, i.e. you didn't make the typo yourself copying it here, then you should report it to your teacher/professor.

 

There's also a typo in part B - 'iter8er' instead of 'itR8R'.

 

So basically I have these multiple questions that were given by my professor and I done several of it but I want to make sure whether these are corrects or not so please kindly help and check if any wrong

 

1. Which of the following strings would match the pattern: Pattern pattern = Pattern.compile("([Mm]e|[Yy}ou).*\\1$");

a. "Is it me or you"

b. "You're just You"

c. "Some come to me"

d. "Are you here for you or me"

e. "Me for you, and you for me"

f. "It's always me, me, me"

 

My answers: b, c, e, f

 

I posted a picture of my work 

2. Answer : C

3. Answer: A

Part B is shown on the link

For Part C please help me because it's unfinished and I do not think i get it right either

 

http://imgur.com/M4g9TBd,tRRpDLH,jd1j38y

 

Your answer to #1 is not quite right. Matching capitalisation is important! I won't tell you the answer outright, as it's better for you to work it out yourself, but that clue should be all you require to get the right answer.

 

I would concur with C for #2.

 

For #3 I really need to brush up on the java language constructs at play here (i.e. generics), so at this present time I can't tell you whether or not you are correct. However note that the question allows for more than one of the examples being correct instantiations (you only selected one, so I'm not certain whether you missed that or actually only felt that one was correct). I am not confident about A being a correct instantiation. There is one other, perhaps two that I feel may be correct, but I may very well be wrong.

 

Your answer to part B is correct, putting aside the fact that the typo I pointed out above would prevent the code from actually compiling.

 

For part C, firstly, I'm not happy with what you've written regarding big-Oh notation. Refer back to question #2; all they're asking for is something along the lines of "O(n)"! (But something applicable to this particular algorithm in place of 'n' here if 'n' is not applicable).

 

As for the code...

 

Do try really hard to not slant the code as you're writing it as doing so impacts readability. Furthermore check your indentation; your for loop seems to have an unnecessary extra indentation, when it should actually be inline with the line above it.

 

You started out somewhat well with your while loop, though as pointed out above, it should be checking that the list is NOT empty not that it is empty - big mistake!

 

Inside the while loop you loose me, part of your code seems to be cut off in the image you've taken of it; I can't make out exactly what you've written in that for loop - is that a splodge of ink next to the 'i' or what?; your for loop syntax is wrong (unless this is actually allowed in java, and I don;t think it is) - there are three things to include in a for loop - initialisation, condition and increment/decrement, each separated with a semi-colon (;) character (some can be left out, but the semi-colon separator is still required); your for loop only executes when i = 0, with no 'i' variable having been declared or initialized anywhere, and with no increment occurring anywhere; and what the hell does "temp = alist B empty" mean? And that's not the end of the problems with it. I'm sorry but this is a mess :/

 

Let's walk through this a little. Your code needs to walk through all items in 'aList' to determine which item has the smallest integer. Once it has found the smallest integer, it needs to remove it from 'aList' and add it to 'bList'. It needs to repeat these actions until aList is empty, and thus all items have been moved to bList, now in numerical order, from smallest to largest value. Your while loop, if corrected to run until aList is empty, fulfils the requirement of repeating the set of actions [find smallest; move it to bList] until all items have been moved to bList, so that's a great start. Within the while loop you simply need code to perform those actions. This code can start by assuming that the first entry in the list is the smallest, and needs a variable to keep track of the index number of the item with the smallest number. It then needs to loop over every entry in the list. On each loop, the value of the current item should be compared with the value of the list entry holding the smallest value found so far. If it is equal or greater, you do nothing, but if it is smaller, you set the current index number as being the index number for the smallest item so far. Your for loop can actually skip over the first entry in the list (i.e. initialise i=1 instead of i=0), since before starting the loop you should assume that the first item is the smallest, and there is no point in comparing the first entry with itself when the looping begins. Your code does not need a variable to hold a copy of the smallest value, only a variable to hold the index of the list entry with the smallest value (unless it provides a desirable performance optimisation to actually hold a copy - something that you can ignore here but you may wish to consider in real life code). After the for loop has found the index of the smallest list entry, you then need to simply perform the action of removing that entry from aList and adding a new entry with the same value to bList (tip: it may be best here to add to bList first, then remove from aList, since this avoids needing a variable to hold a copy of the value).

Link to comment
Share on other sites

  • 0

So basically I have these multiple questions that were given by my professor and I done several of it but I want to make sure whether these are corrects or not so please kindly help and check if any wrong

 

1. Which of the following strings would match the pattern: Pattern pattern = Pattern.compile("([Mm]e|[Yy}ou).*\\1$");

a. "Is it me or you"

b. "You're just You"

c. "Some come to me"

d. "Are you here for you or me"

e. "Me for you, and you for me"

f. "It's always me, me, me"

 

My answers: b, c, e, f

Unless I'm misunderstanding the expression (quite possible given my limited regexp knowledge), shouldn't it only match a starting Me/You (capitial or lower case M/Y) and ending with the same pattern ('\1' is the same pattern matched in group 1)? So according to my understanding only b fits. I wrote a quick java test and it agreed:

import java.util.regex.*;

public class recheck {

	public static void main ( String [] args ) {
		
		String [] matchers = { "Is it me or you", "You're just You", "Some come to me", "Are you here for you or me", "Me for you, and you for me", "It's always me, me, me" }; 

		Pattern p = Pattern.compile ( "([Mm]e|[Yy]ou).*\\1$" ); 
		
		for ( String matcher : matchers ) {
			Matcher m = p.matcher ( matcher );
			if ( m.matches () )
				System.out.format ( "Matcher '%s' matches the pattern\n", matcher );
			else
				System.out.format ( "Matcher '%s' does not match the pattern\n", matcher );
		}
	}	
}	
$ java recheck 
Matcher 'Is it me or you' does not match the pattern
Matcher 'You're just You' matches the pattern
Matcher 'Some come to me' does not match the pattern
Matcher 'Are you here for you or me' does not match the pattern
Matcher 'Me for you, and you for me' does not match the pattern
Matcher 'It's always me, me, me' does not match the pattern
Link to comment
Share on other sites

This topic is now closed to further replies.