Skip to content

Instantly share code, notes, and snippets.

@23maverick23
23maverick23 / combineTwoLists.py
Last active December 12, 2015 08:18
Python: Combine two lists
#!/usr/bin/env python
def combine_list(list1, list2, short=False):
"""
Accepts 2 lists and returns a combined list mapping
all values in list1 to values in list2. None values
are replaced with an empty string.
list1, list2: valid list objects
short: True or False; stop at shortest list, else longest
@23maverick23
23maverick23 / stripSpacesXMLTags.py
Last active January 1, 2023 14:03
Python: Strip spaces between tags
#!/usr/bin/env python
print re.sub('\s+(?=<)', '', xml_string)
# This regex will turn a prettyprinted XML string into a spaceless
# string which is sometimes easier to deal with.
# Example response from xml.etree.ElementTree.fromstring(xml)
# <response>
# <Auth status="0"/>
@23maverick23
23maverick23 / cubes.js
Created July 27, 2013 15:55
JavaScript: Great example of a for loop in javascript. Note the "_len" variable which stores the list length rather than calling .length on each pass through the loop.
cubes = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = list.length; _i < _len; _i++) {
num = list[_i];
_results.push(math.cube(num));
}
return _results;
})();
@23maverick23
23maverick23 / getMaxOrMin.js
Last active December 20, 2015 14:28
JavaScript: Simple min/max methods for javascript arrays.
// Credit: http://ejohn.org/blog/fast-javascript-maxmin/
Array.max = function(array){
return Math.max.apply(Math, array);
};
Array.min = function(array){
return Math.min.apply(Math, array);
};
@23maverick23
23maverick23 / ternary.js
Created August 17, 2013 11:49
JavaScript: Ternary operation (IF statement shorthand)
/*
* Ternary format: variable = (condition) ? true-value : false-value;
*/
var myName = 'Ryan';
// >>> 'yes'
(myName == 'Ryan') ? 'yes' : 'no';
@23maverick23
23maverick23 / list_to_string.py
Created September 1, 2013 14:11
Python: Convert a set, tuple or list into a comma separated string.
# create a new set and add values
myset = set()
myset.add(1)
myset.add(2)
myset.add(4)
# by default, a set returns a set Object
# myset = set([1, 2, 4])
# use join() to parse the List into a str
@23maverick23
23maverick23 / randomString.js
Created September 11, 2013 22:52
JavaScript: Generates a random 15 character string of alphanumeric characters
// generates a random 15 character string of alphanumeric characters
var str = Math.random().toString(36).slice(2);
@23maverick23
23maverick23 / getDaysBetween.js
Created September 18, 2013 18:06
JavaScript: Returns the number of days between 2 date objects
function getDaysBetween(dStartDate, dEndDate) {
var intOneDay = 1000 * 60 *60 * 24;
return Math.ceil((dEndDate.getTime() - dStartDate.getTime()) / intOneDay);
}
@23maverick23
23maverick23 / daysInMonth.js
Last active December 23, 2015 09:19
JavaScript: Return the number of days in a month
function daysInMonth(iMonth, iYear) {
return new Date(iYear, iMonth, 0).getDate();
}
@23maverick23
23maverick23 / simpleCalendar.py
Created October 14, 2013 21:05
Python: Print a simple, formatted calendar to the console
import calendar
print 'Print a calendar for any month!'
month = int(raw_input('Month (mm)? '))
year = int(raw_input('Year (yyyy)? '))
print '\n'
calendar.setfirstweekday(calendar.SUNDAY)
cal = calendar.monthcalendar(year, month)