Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits in a given base. Here we will restrict ourselves to decimal (base 10).

For example, take 153 (3 digits):
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

and 1634 (4 digits):
1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634

The Challenge:

Your code must return true or false depending upon whether the given number is a Narcissistic number in base 10.

NOTE: Error checking for text strings or other invalid inputs is not required, only valid integers will be passed into the function.

#python #question #codewars
Tech C**P
A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits in a given base. Here we will restrict ourselves to decimal (base 10). For example, take 153 (3 digits): 1^3 + 5^3 + 3^3 = 1 + 125…
from math import pow

def narcissistic(value):
digits = map(int, list(str(value)))
power = len(digits)
final_res = 0
for digit in digits:
final_res += pow(digit, power)
return int(value) == int(final_res)

#python #solution
The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
# should be 6: [4, -1, 2, 1]

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.


#python #codewars
Babel:
Babel is an integrated collection of utilities that assist in internationalizing and localizing Python applications, with an emphasis on web-based applications.

pip install Babel

More information:
- http://babel.pocoo.org/en/latest/index.html

#python #pocoo #pip #babel #internationalization #localization
How do you sort the below object based on the value in Javascript?

{
"en": "English (English)",
"fa": "French",
"ar": "العربیه",
"zh": "中文 (Chinese)"
}

Well first you need to make an array of arrays:

var sortable_langs = [];
$.each(languages, function(index, language) {
sortable_langs.push([index, language]);
});

sortable_langs.sort(function(a, b) {
if(a[1] < b[1]) { return -1; }
if(a[1] > b[1]) { return 1; }
return 0;
});

NOTE: a[1] and b[1] refers to the language value like: English (English). They are compared with each other. If a[1] is bigger than b[1] then it will be moved to the first to the top.

#javascript #sort #alphabetically
In-place value swapping

# Let's say we want to swap
# the values of a and b...
a = 23
b = 42

# The "classic" way to do it
# with a temporary variable:
tmp = a
a = b
b = tmp

# Python also lets us
# use this short-hand:
a, b = b, a

#python #swap #tricks
How to display a 3 dots for a long text in span?

span {
width: 110px;
display: inline-block;
white-space: nowrap;
overflow: hidden !important;
text-overflow: ellipsis;
}

#css #span #3dots
How to get only numbers in textbox using jQuery?

// Restricts input for each element in the set of matched elements to the given inputFilter.
(function($) {
$.fn.inputFilter = function(inputFilter) {
return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
if (inputFilter(this.value)) {
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
}
});
};
}(jQuery));

// restrict input to receive just numbers
$("#my_input_num").inputFilter(function(value) {
return /^\d*$/.test(value);
});

#javascript #jquery #input #number
In Google OAUTH 2.0 in case you get a token from Google Login you can get user's information for that token by sending a simple GET request to the following URL:

https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=YOUR_GOOGLE_TOKEN

The response differs based on the given scope on the first login step. Mine was set to the below:

https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email

#google #login_with_google #oauth #scope #userinfo #oauth2
Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

Examples:

pig_it('Pig latin is cool') # igPay atinlay siay oolcay
pig_it('Hello world !') # elloHay orldway !

#question #codewars #algorithm
How to find the query associated with a queryset in Django ORM?

Sometime you want to know how a Django ORM makes our queries execute or what is the corresponding SQL of the code you are writing. This is very strightforward. Youn can get str of any queryset.query to get the sql.

You have a model called Event. For getting all records, you will write something like Event.objects.all(), then do str(queryset.query)


>>> queryset = Event.objects.all()
>>> str(queryset.query)
SELECT "events_event"."id", "events_event"."epic_id",
"events_event"."details", "events_event"."years_ago"
FROM "events_event"



Example 2:

>>> queryset = Event.objects.filter(years_ago__gt=5)
>>> str(queryset.query)
SELECT "events_event"."id", "events_event"."epic_id", "events_event"."details",
"events_event"."years_ago" FROM "events_event"
WHERE "events_event"."years_ago" > 5


#python #django #orm
How to convert ByteArray to PDF and then upload it via jQuery?

var docData = [ yourByteArray ];
var blob = new Blob([new Uint8Array(docData)], { type: 'application/pdf' });

// Now create form to upload the file
var formData = new FormData();
formData.append("file", blob);

// Let's now upload the file
$.ajax({
type: 'POST',
url: 'https://www.YOUR-UPLOAD-FILE-ENDPOINT.com/storage',
beforeSend: request => set_ajax_headers(request),
data: formData,
processData: false,
contentType: false
}).done(function(data) {
console.log('File is uploaded!');
});

NOTE: function set_ajax_headers is a function that sets headers on the given request.

#javascript #ByteArray #PDF #Uint8Array #Blob #jQuery
Write a function that when given a URL as a string, parses out just the domain name and returns it as a string. For example:

domain_name("http://github.com/carbonfive/raygun") == "github" 
domain_name("http://www.zombie-bites.com") == "zombie-bites"
domain_name("https://www.cnet.com") == "cnet"

#python #codewars
MongoDB server Load Average: 0.5 (It can reach 16)
Database Size: 100GB (It is compressed in MySQL it reaches 300 GB in size!)
Req/Sec: 500

Our server seems hungry for more requests and more data.

#mongodb #mongo #awesomeness
You are given a node that is the beginning of a linked list. This list always contains a tail and a loop.

Your objective is to determine the length of the loop.

For example in the following picture the tail's size is 3 and the loop size is 11: