Confused by ...()

Confused with Def Python

You forgot to put () to string function.

def yesno(endwhat):
end = False
while not end:
ok = input("<Y/N>")
if ok.upper() == 'Y' or ok.upper() == 'N':
if ok.upper() == 'Y':
end = True
endwhat = True
else:
print("Enter only <Y/N>")
return(ok)

Javascript function confused syntax

  1. The empty function is a default value for done. Default values prevents runtime crashes.

2 and 3 can be understood by seeing below code: (simply run it and see the consoles.

const DEFAULT_FUNCTION_VALUE = ()=> {};

const XXXX = {
load: function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve({data: 'from XXXX.load'});
},2000);
});
}
}

const Index = function(name='', done=DEFAULT_FUNCTION_VALUE) {
return function(dispatch, getState) {
return XXXX.load().then(function(result) {
console.log({result});
done(result);
}).catch(function(error) {
console.log(error);
});
}
}

function doneImplementation(data) {
console.log('Data from done- ', data);
}

Index('', doneImplementation)();

[python]: confused by super()

super() returns an instance of the base class, so self gets implicitly passed to __init__() like in any other method call.

With regards to your second question, that's correct. Calling super() without an instance as the second argument returns a reference to the class itself, not an instance constructed from your subclass instance.

Python Functions Confusion

Python is case-sensitive. The two lines below are referring to two different variables (note the capital "N" in the first one):

def readwrite(fileName, list):
print 'arg file=',filename

What's happening is that the second line picks up the global variable called filename instead of the function argument called fileName.

Confused with an SQL query

Yea first start by grouping user1, user2, & message. This gives you a unique message for every pair:

SELECT case when recipient > sender then recipient else sender end user1,
case when recipient > sender then sender else recipient end user2,
message
FROM Messages
GROUP BY user1, user2, message

Then from that result group by Message and return only count greater than 1. You can use a nested query to do this:

SELECT message, COUNT(message) 
FROM (SELECT case when recipient > sender then recipient else sender end user1,
case when recipient > sender then sender else recipient end user2,
message
FROM Messages
GROUP BY user1, user2, message) PairMessages
GROUP BY message
HAVING COUNT(message) > 1

Maybe start with this as a test:

INSERT INTO Users VALUES (1,'john',1111111111)
INSERT INTO Users VALUES (2,'paul',2222222222)
INSERT INTO Users VALUES (75,'george',7575757575)
INSERT INTO Users VALUES (83,'ringo',8383838383)
INSERT INTO Messages VALUES (2,1,GETDATE(),'Yesterday')
INSERT INTO Messages VALUES (1,2,GETDATE(),'hello')
INSERT INTO Messages VALUES (75,83,GETDATE(),'yellow')
INSERT INTO Messages VALUES (75,83,GETDATE(),'hello')

You should be able to get hello as your message sent between more than 1 pair of users.

Edit: I updated the above with the correct answer to show that each pair of users is unique for each message. Also, it may be a good idea to create a groupID for every pair of users. Then you can add as many users as you want to that groupID. See here for an idea: http://sqlfiddle.com/#!9/fbc2e2/3

Confused at the filter() function and I need an in depth explanation

filter doesn't return a boolean, it returns an array containing elements of the original array. It decides which elements to include in the result by calling a function that you supply on each element of the array. This function returns a boolean: if it's truthy, that element is included in the result. The arguments to the function are the element (called item in your function) and its position in the array (called pos).

This function uses indexOf() to find the position of the first element in the array that has the same value as the element it was given. If that position is the same as the position it was given, then this element must be the first occurrence of that value in the array, so it returns true and the element will be included in the result. If the positions are different, then this element must be a later duplicate, so it returns false and the element is not included in the result. The final result of this is that only the first copy of each value is included in the resulting of filtering, which means they're all unique.

For instance, suppose the array is:

["a", "b", "a", "c"]

On the first iteration, item = "a" and pos = 0. a.indexOf(item) returns 0, and the function returns 0 == 0, which is true, so item is included in the result.

On the second iteration, item = "b" and pos = 1. a.indexOf(item) returns 1, and the function returns 1 == 1, which is true, so item is included in the result.

On the third iteration, item == "a" and pos = 2. a.indexOf(item) returns 0, and the function returns 0 == 2, which is false, so item is not included in the result.

On the fourth iteration, item = "c" and pos = 3. a.indexOf(item) returns 3, and the function returns 3 == 3, which is true, so item is included in the result.

The final result is that the items from the first, second, and fourth iterations are included in the result, which is ["a", "b", "c"].

Confusion of nested ifelse expression

This code aims to define a new column, Y in the data set x. The column Y will populate based on the following statements:
Sample Image



Related Topics



Leave a reply



Submit