Enable contrast version

Tutor profile: David A.

Inactive
David A.
All-purpose nerd
Tutor Satisfaction Guarantee

Questions

Subject: Python Programming

TutorMe
Question:

Why do I get an error message when I do this? s1 = "exemple string" print ( s1[2] ) //prints 'e' . Remember to start counting at 0. s1[2] = 'a' //TypeError...

Inactive
David A.
Answer:

Before answering the question, it's worth looking at the error message you get. It should end with something like "TypeError: 'str' object does not support item assignment". It should also include a file name and line number. Note that compilers sometimes get a little confused and may not notice an error until a line or two after the real problem, but in this case we know that the problem is in the last line, since we got correct output from the previous line. The error message also tells us the problem has something to do with "assignment", which makes sense: the last line is an assignment statement (it uses the "=" assignment operator). Now that we're confident the compiler is looking at the right line, it would be nice if we could understand the error message. TypeError is the category: it's an error that has something to do with types. Maybe we've tried to assign something where it's not supposed to go, because it's the wrong type. What's a 'str' object? It turns out that 'str' is Python's internal name for its built-in string type. Try doing this in a python program: print ( type ( "hello" ) ) //prints "<class 'str'>" So what the error message is trying to tell you is that you can't do "item assignment" to a string. In other words, in Python, _strings are immutable_. Once you create a string, you can't change it. Wait, wait, you say. What about this: my_string = "string1" my_string = "string2" //perfectly legal. To understand this, you have to understand the difference between a variable and an object. A variable is a name, that refers to an object. my_string is a variable, and after the first line above it refers to a string object "string1". After the second line, it refers to a new, different string object "string2". We've changed the variable my_string, but we didn't change either of the string objects. A good way to understand mutability is to practice with lists and tuples. In Python, a list is mutable but a tuple is not. Try this: x = [1,2,3] // new list print(x[1]) // prints "2" x[1] = 5 // item assignment, perfectly OK x = (1,2,3) // new tuple print(x[1]) //prints "2" x[1] = 5 //TypeError If you think you've got this, try making lists of tuples, or tuples of lists, and then try to modify them. For example: my_list_of_tuples = [(1,2,3),(21,22,23)] my_list_of_tuples[0][1] = 5 // legal or not?

Subject: Writing

TutorMe
Question:

What is the most important question a writer or editor, or a writing coach, should ask before starting to write, or revise, or critique, a work?

Inactive
David A.
Answer:

"Who is the audience?" The way you write a love letter is not going to be the same as the way you write a legal brief, or a user's manual, or a novel, or an advertising slogan. There are, of course, certain strategies that work in almost any form of writing and for almost any audience. Grab their attention early. Relate your work to something they care about. If you're asking for something, be direct and crystal clear about what you want, when you want it, and why your reader should give it to you. Be as brief and concise as possible. Add some variation and some humor, especially if it's impossible to be brief. Relate your work to something the audience cares about, again and again. If you're writing a business letter, expect that most of your audience will not read beyond the first page, and probably not beyond the first paragraph. Your "bottom line" should be in the top line, and also in the Subject line if it's not confidential. "I need the quarterly report by Friday or we're going to lose the Johnson contract." You've gotten their attention early, you're relating to something they (should) care about, and you've made a request that was completely clear and direct. Frankly, the rest of the letter could almost be left blank, but you can use it to summarize the history of how you got to this state, in case your audience wasn't paying attention to your earlier communications (and you can bet that most of them weren't). If you're writing a novel, brevity is less essential, but you still need to get your audience's attention. If there's a central mystery or conflict, try to foreshadow it in the first two pages. (Or just start _in media res_, it's a classic structure because it works.) Don't indulge in pages of back-story. Anything that's important to the main story, will come up in the course of the story! When reviewing a writing, I usually emphasize structure, because there's not much point in scrutinizing things at the paragraph or sentence level if the whole page or chapter needs to be re-written or deleted. But I can also help with style and grammar. For example, I tell many writers to be suspicious of adjectives and adverbs. A shorter sentence with fewer decorations is usually more forceful and more memorable. In non-fiction, a good structure is often "tell 'em what you're going to tell 'em, then tell 'em, then tell 'em what you told 'em." Here I've used mostly short, direct sentences to give examples of different writing strategies for different audiences, while also giving advice that should be applicable to almost any audience. It's my intent that you, my reader, will see for yourself how these examples and advice relate to your current writing difficulties. I do wish I'd worked in more excitement and humor, but I have one more piece of advice for you: sometimes completing a work is more important than perfecting it. Writing isn't always easy, no matter how much you practice, and deadlines have a way of creeping up on you. Knowing when to stop is a very valuable skill, that I can teach. My request to you: go click on the thingy and sign up for some coaching with me right now. You'll be glad you did.

Subject: Computer Science (General)

TutorMe
Question:

What is the complexity (big-O) of the algorithm pseudocoded below? Could the algorithm be improved? If so, how? function find (list, target_value) { for i in ( 0 .. length(list) -1): { if (list[i] == target_value): return TRUE } return FALSE }

Inactive
David A.
Answer:

In the worst case, this algorithm searches once through the entire list (or half the list in the average case), which takes time proportional to the length of the list. That's O(n). That's the best you can expect on an arbitrary list, but in special circumstances you can get faster searches. It is sometimes worthwhile to rewrite other parts of your program to create those circumstances. In particular, if you don't need to preserve the original order of the list, and the list changes infrequently compared to how often you need to search it, then it can be a good idea to reorganize the list to make it easier to search. For example, if we know that the list is sorted, and can be accessed randomly, then we can do a binary search within the list, which takes only O(log(n)). The time needed to sort the list is O(n*log(n)), but this can be a good trade-off if the list changes infrequently and so doesn't need to be re-sorted very often. Or, we can get even faster look-up by storing the list as a hash table. That allows lookups in O(1) time. This works in your favor if you do lookups much more often than you need to build the hash table - in fact it pays off pretty quickly, since building an entire hash table is only an O(n) operation, and finds, inserts and deletes are O(1) [for fixed-size hash tables]. One drawback is that hashes use more memory than lists. Neither of those ideas works well then the list is storing something that shouldn't be re-ordered: a string of text, for example. You *could* solve this by keeping two (or more!) copies of your list data, in different forms for different purposes, but that gets complicated (leading to bugs), and uses lots of memory. Neither idea is any improvement if you're searching in a new list every time, either. Restructuring the data is O(n) or longer, before you can do a lookup. For arbitrary data, an O(n) algorithm, as in pseudocode in the question, is the best you can do. --- If you're just starting a unit on big-O notation, the first step is to recognize the pseudocode as O(n): you're looping through the list once. Next you'll want to understand why searching an arbitrary list cannot go any faster: you have to search the whole list. A question about big-O notation will almost always have something to do with searching and/or sorting, which usually leads to some discussion about binary search and/or binary trees. So, assuming you've covered those in your class, when you see the hint in the question that there might be a possible improvement to this algorithm, you should mention that you've thought about binary search. Either write down why you don't think it applies to this question (list cannot be assumed to be sorted) or why it is or isn't a good idea (sorting the list takes time). This is your chance to casually mention that binary search runs in O(log(n)) time, and that a good sorting algorithm runs in O(n*log(n)) time. Feel free to mention a couple of good sort algorithms here (heapsort, quicksort) but don't spend any time on them. You can expect to deal with them in a separate question later in the same exam. Rule of thumb: don't write any code if the question doesn't ask for it. Code you don't write can't distract you with bugs. You may not have covered hash tables yet; if not, don't worry about that paragraph. If so, you should know why hash lookups are O(1) (the hash function is a slow step, but it's always only one step, regardless of N). (That's a simplification; if you know about hash collisions, give yourself 10 bonus points! Ten more if you know about hash table resizing.) Mention at least one drawback to hash tables (memory use, lack of ordering). It's good to wrap up by repeating your main point(s): the pseudocode is O(n), and that's the best you can do without making additional assumptions.

Contact tutor

Send a message explaining your
needs and David will reply soon.
Contact David

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.