Assume that BlogComment is a structured type with these fields, comment (a strin
ID: 441691 • Letter: A
Question
Assume that BlogComment is a structured type with these fields, comment (a string, the actual comment), and two int fields: like, dislike which count the number of "likes" and "dislikes" of the comment by visitors. Assume that nComments is an int variable that holds the length of an array named blogComments whose elements are of type BlogComment. This array has been declared and initialized. You may assume that the array is not empty. Assume that an string variable mostControversial has been declared. Write the necessary code that traverses the blogComments array and find the entry that is most controversial and assign its comment field to mostControversial. Measure the degree of controversy by multiplying likes and dislikes. (See how it works? Imagine 10 comments. If that is 10 likes and no dislikes, it is not controversial and 10 times 0 is 0. But if it is evenly split, 5 and 5, then it is controversial, and 5 times 5 is 25... a lot more than 0.) _________________________________________________________________________ My code: int temp = 0; int cont = 0; int mcindex = 0; for (int index = 0; index < nComments; index++) { cont = (blogComments[index].like * blogComments[index].dislike); if (cont > temp) { temp = cont; mostControversial = index; } } blogComments[mcindex].comment = mostControversial;Explanation / Answer
This is the simplest Redis type. If you use only this type, Redis will be something like a memcached server with persistence. Let's play a bit with the string type: $ redis-cli set mykey "my binary safe value" OK $ redis-cli get mykey my binary safe value As you can see using the SET command and the GET command is trivial to set values to strings and have the strings returned back. Values can be strings (including binary data) of every kind, for instance you can store a jpeg image inside a key. A value can't be bigger than 512 MB. Even if strings are the basic values of Redis, there are interesting operations you can perform against them. For instance, one is atomic increment: $ redis-cli set counter 100 OK $ redis-cli incr counter (integer) 101 $ redis-cli incr counter (integer) 102 $ redis-cli incrby counter 10 (integer) 112 The INCR command parses the string value as an integer, increments it by one, and finally sets the obtained value as the new string value. There are other similar commands like INCRBY, DECR and DECRBY. Internally it's always the same command, acting in a slightly different way. What does it mean that INCR is atomic? That even multiple clients issuing INCR against the same key will never incur into a race condition. For instance it can never happen that client 1 read "10", client 2 read "10" at the same time, both increment to 11, and set the new value of 11. The final value will always be 12 and the read-increment-set operation is performed while all the other clients are not executing a command at the same time. Another interesting operation on string is the GETSET command, that does just what its name suggests: Set a key to a new value, returning the old value as result. Why this is useful? Example: you have a system that increments a Redis key using the INCR command every time your web site receives a new visit. You want to collect this information one time every hour, without losing a single key. You can GETSET the key, assigning it the new value of "0" and reading the old value back. The List type To explain the List data type it's better to start with a little bit of theory, as the term List is often used in an improper way by information technology folks. For instance "Python Lists" are not what the name may suggest (Linked Lists), they are actually Arrays (the same data type is called Array in Ruby actually). From a very general point of view a List is just a sequence of ordered elements: 10,20,1,2,3 is a list. But the properties of a List implemented using an Array are very different from the properties of a List implemented using a Linked List. Redis lists are implemented via Linked Lists. This means that even if you have millions of elements inside a list, the operation of adding a new element in the head or in the tail of the list is performed in constant time. Adding a new element with the LPUSH command to the head of a ten elements list is the same speed as adding an element to the head of a 10 million elements list. What's the downside? Accessing an element by index is very fast in lists implemented with an Array and not so fast in lists implemented by linked lists. Redis Lists are implemented with linked lists because for a database system it is crucial to be able to add elements to a very long list in a very fast way. Another strong advantage is, as you'll see in a moment, that Redis Lists can be taken at constant length in constant time. First steps with Redis lists The LPUSH command adds a new element into a list, on the left (at the head), while the RPUSH command adds a new element into a list, on the right (at the tail). Finally the LRANGE command extracts ranges of elements from lists: $ redis-cli rpush messages "Hello how are you?" OK $ redis-cli rpush messages "Fine thanks. I'm having fun with Redis" OK $ redis-cli rpush messages "I should look into this NOSQL thing ASAP" OK $ redis-cli lrange messages 0 2 1. Hello how are you? 2. Fine thanks. I'm having fun with Redis 3. I should look into this NOSQL thing ASAP Note that LRANGE takes two indexes, the first and the last element of the range to return. Both the indexes can be negative to tell Redis to start to count from the end, so -1 is the last element, -2 is the penultimate element of the list, and so forth. As you can guess from the example above, lists could be used in order to implement a chat system. Another use is as queues in order to route messages between different processes. But the key point is that you can use Redis lists every time you require to access data in the same order they are added. This will not require any SQL ORDER BY operation, will be very fast, and will scale to millions of elements even with a toy Linux box. For instance in ranking systems like that used by social news site reddit.com you can add every new submitted link into a List, and with LRANGE it's possible to paginate results in a trivial way. In a blog engine implementation you can have a list for every post, where to push blog comments, and so forth. Pushing IDs instead of the actual data in Redis lists In the above example we pushed our "objects" (simply messages in the example) directly inside the Redis list, but this is often not the way to go, as objects can be referenced in multiple times: in a list to preserve their chronological order, in a Set to remember they are about a specific category, in another list but only if this object matches some kind of requisite, and so forth. Let's return back to the reddit.com example. A better pattern for adding submitted links (news) to the list is the following: $ redis-cli incr next.news.id (integer) 1 $ redis-cli set news:1:title "Redis is simple" OK $ redis-cli set news:1:url "http://code.google.com/p/redis" OK $ redis-cli lpush submitted.news 1 OK We obtained a unique incremental ID for our news object just incrementing a key, then used this ID to create the object setting a key for every field in the object. Finally the ID of the new object was pushed on the submitted.news list. This is just the start. Check the command reference and read about all the other list related commands. You can remove elements, rotate lists, get and set elements by index, and of course retrieve the length of the list with LLEN. Redis Sets Redis Sets are unordered collection of binary-safe strings. The SADD command adds a new element to a set. It's also possible to do a number of other operations against sets like testing if a given element already exists, performing the intersection, union or difference between multiple sets and so forth. An example is worth 1000 words: $ redis-cli sadd myset 1 (integer) 1 $ redis-cli sadd myset 2 (integer) 1 $ redis-cli sadd myset 3 (integer) 1 $ redis-cli smembers myset 1. 3 2. 1 3. 2 I added three elements to my set and told Redis to return back all the elements. As you can see they are not sorted. Now let's check if a given element exists: $ redis-cli sismember myset 3 (integer) 1 $ redis-cli sismember myset 30 (integer) 0 "3" is a member of the set, while "30" is not. Sets are very good for expressing relations between objects. For instance we can easily use Redis Sets in order to implement tags. A simple way to model this is to have a Set for every object containing its associated tag IDs, and a Set for every tag containing the object IDs that have that tag. For instance if our news ID 1000 is tagged with tag 1,2,5 and 77, we can specify the following five Sets - one Set for the object's tags, and four Sets for the four tags: $ redis-cli sadd news:1000:tags 1 (integer) 1 $ redis-cli sadd news:1000:tags 2 (integer) 1 $ redis-cli sadd news:1000:tags 5 (integer) 1 $ redis-cli sadd news:1000:tags 77 (integer) 1 $ redis-cli sadd tag:1:objects 1000 (integer) 1 $ redis-cli sadd tag:2:objects 1000 (integer) 1 $ redis-cli sadd tag:5:objects 1000 (integer) 1 $ redis-cli sadd tag:77:objects 1000 (integer) 1 To get all the tags for a given object is trivial:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.