[Solved] Find All Possible Fixed Size String Python

[ad_1] You can use itertools.product for this. It returns a generator of sequences of fixed length. As you order your strings first by length and then lexicographically, you may use something like this import itertools for l in range(1, 5): for seq in itertools.product(“abc”, repeat=l): print(“”.join(seq)) 3 [ad_2] solved Find All Possible Fixed Size String … Read more

[Solved] applying onehotencoder on numpy array

[ad_1] Don’t use a new OneHotEncoder on test_data, use the first one, and only use transform() on it. Do this: test_data = onehotencoder_1.transform(test_data).toarray() Never use fit() (or fit_transform()) on testing data. The different number of columns are entirely possible because it may happen that test data dont contain some categories which are present in train … Read more

[Solved] Using the List Feature in Python

[ad_1] def days_in_month(month): for m, nblist in month_days: if month==m: return nblist else: return [] days_in_month(‘May’) Out[20]: [31] days_in_month(‘Mady’) Out[21]: [] [ad_2] solved Using the List Feature in Python

[Solved] Summing the digits of a very large number [closed]

[ad_1] This would be a complete implementation in Haskell import Data.List.Split (chunksOf) import Data.List import Data.Char (digitToInt) main :: IO () main = do digits <- map digitToInt <$> readFile “pifile.txt” let summator = foldl1′ (zipWith (+)) . chunksOf 4 print $ summator digits I will update this with some explanation later this day. Update … Read more

[Solved] Words, sorted by frequency, in a book (.txt file)

[ad_1] if you want to write it in a sorted manner you can do this. from collections import Counter wordlist = open(‘so.py’, ‘r’).read().split() word_counts = Counter(wordlist) write_file = open(‘wordfreq.txt’, ‘w’) for w, c in sorted(word_counts.iteritems(), key=lambda x: x[1], reverse=True): write_file.write(‘{w}, {c}\n’.format(w=w, c=c)) [ad_2] solved Words, sorted by frequency, in a book (.txt file)

[Solved] What would the ruby equivalent be to this python script?

[ad_1] Python: print (“{0:5s} {1:7s} {2:9s} {3:6s} {4:25s} {5:s}”.format(‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’)) Ruby: puts “%-5s %-7s %-9s %-6s %-25s %-5s” % [‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’] Alternatively: puts sprintf(“%-5s %-7s %-9s %-6s %-25s %-5s”, *[‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’]) 1 [ad_2] solved What would the ruby equivalent be to this python … Read more

[Solved] function with arguments in python [closed]

[ad_1] def count(word, letter): count = 0 for l in word: if l == letter: count = count + 1 return count word = raw_input(‘Enter a string:’) letter = raw_input(‘Enter a character:’) print count(word, letter) [ad_2] solved function with arguments in python [closed]

[Solved] Web Scraping particular tags using Python [closed]

[ad_1] There is an incredible scraping library for Python called BeautifulSoup which will make your life much easier: http://www.crummy.com/software/BeautifulSoup/ BeautifulSoup allows you to select by html tags and/or html attributes such via a css class name. It also handles bad html docs really well but you need to read the docs on how it works. … Read more

[Solved] Any idea why hackerrank doesn’t accept my code even if it prints exactly what is asked

[ad_1] You have an extra line of spaces on the top Try this- import math import os import random import re import sys # Complete the staircase function below. def staircase(n): for x in range(1,n+1): print((n-x)*” “+”#”*x) if __name__ == ‘__main__’: n = int(input()) staircase(n) [ad_2] solved Any idea why hackerrank doesn’t accept my code … Read more