Skip to content

Instantly share code, notes, and snippets.

View tyler-austin's full-sized avatar

Tyler Austin tyler-austin

  • Scale AI
  • Washington DC
  • 18:11 (UTC -04:00)
View GitHub Profile
@tyler-austin
tyler-austin / arcpy_field_names.py
Created May 30, 2017 20:56
arcpy field names
import arcpy
vector_file = os.path.join(r"/path/to/feature/class")
field_names = [f.name for f in arcpy.ListFields(vector_file)]
field_names
# [u'FID', u'Shape', u'Id']
@tyler-austin
tyler-austin / unique_field_values.py
Created May 30, 2017 21:12
arcpy unique field values
import arcpy
import os
def unique_values(vector_data, field):
with arcpy.da.SearchCursor(vector_data, [field]) as cursor:
return sorted({row[0] for row in cursor})
vector_data = os.path.join(r"/path/to/feature/class")
@tyler-austin
tyler-austin / print_first_x_rows.py
Created May 30, 2017 21:37
arcpy print first x rows
import arcpy
import os
def _print_top_rows(vector_data, num_rows=10):
field_names = [f.name for f in arcpy.ListFields(vector_data)]
print field_names
i_row = 0
with arcpy.da.SearchCursor(vector_data, ['*']) as cursor:
for row in cursor:
@tyler-austin
tyler-austin / create_scratch_fgdb.py
Last active May 30, 2017 22:46
arcpy create scratch fgdb
import arcpy
def set_scratch_environment(self, scratch_dir, cpu_core):
self.core_scratch_dir = os.path.join(scratch_dir, 'scratch_{0}'.format(cpu_core))
os.mkdir(self.core_scratch_dir)
arcpy.env.scratchWorkspace = self.core_scratch_dir
arcpy.env.workspace = arcpy.env.scratchGDB
self.scratch_gdb = os.path.join(self.core_scratch_dir, 'scratch.gdb')
@tyler-austin
tyler-austin / strip_unwanted_punctuation.py
Created May 31, 2017 19:42
strip unwanted punctuation
PUNCTUATION = '!"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~'
def remove_punctuation(in_string):
for punc in PUNCTUATION:
in_string = in_string.replace(punc, '')
return in_string
bad_string = 'I like (love) 2 make - life hard _ 4 my programmers #$AD!!!!'
bad_string = remove_punctuation(bad_string)
@tyler-austin
tyler-austin / README.md
Last active May 31, 2017 21:37
Code Fights - matrixElementsSum

After becoming famous, CodeBots decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms, each cell containing an integer - the price of the room. Some rooms are free (their cost is 0), but that's probably because they are haunted, so all the bots are afraid of them. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable for the bots.

Help the bots calculate the total price of all the rooms that are suitable for them.

Example

For

matrix = [[0, 1, 1, 2], 
 [0, 5, 0, 0], 
@tyler-austin
tyler-austin / README.md
Last active May 31, 2017 21:49
Code Fights - allLongestStrings

Given an array of strings, return another array containing all of its longest strings.

Example

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

Input/Output

  • [time limit] 4000ms (py3)
@tyler-austin
tyler-austin / README.md
Created May 31, 2017 22:02
Code Fights - commonCharacterCount

Given two strings, find the number of common characters between them.

Example

For s1 = "aabcc" and s2 = "adcaa", the output should be
commonCharacterCount(s1, s2) = 3.

Strings have 3 common characters - 2 "a"s and 1 "c".

Input/Output

@tyler-austin
tyler-austin / README.md
Created June 1, 2017 17:47
Code Fights - reverseParentheses

You have a string s that consists of English letters, punctuation marks, whitespace characters, and brackets. It is guaranteed that the parentheses in s form a regular bracket sequence.

Your task is to reverse the strings contained in each pair of matching parentheses, starting from the innermost pair. The results string should not contain any parentheses.

Example

For string s = "a(bc)de", the output should be
reverseParentheses(s) = "acbde".

Input/Output

@tyler-austin
tyler-austin / README.md
Created June 1, 2017 18:15
Code Fights - alternatingSums

Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.

You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.

Example

For a = [50, 60, 60, 45, 70], the output should be
alternatingSums(a) = [180, 105].

Input/Output