Skip to content

Instantly share code, notes, and snippets.

View abdulateef's full-sized avatar

Abdulateef abdulateef

  • sekat technologies
  • Lagos Nigeria
View GitHub Profile
def longest(words):
tokens = words.split()
max_length = 0
max_word=""
for token in tokens:
if len(token)> max_length:
max_length = len(token)
max_word = token
return max_word
@abdulateef
abdulateef / power.py
Created September 12, 2017 11:34
Write a function named power that accepts two arguments, a and b and calculates a raised to the power b.
def power(a,b):
if b == 0:
return 1
else:
return eval(((str(a)+"*")*b)[:-1])
@abdulateef
abdulateef / my_sort.py
Last active December 9, 2018 11:26
Write a function my_sort which takes in a list of numbers (integers). The function should return a list of sorted numbers such that odd numbers come first and even numbers come last.
def my_sort(numbers):
odd = [n for n in numbers if n % 2 != 0]
even = [n for n in numbers if n % 2 == 0]
return sorted(odd) + sorted(even)
print( my_sort([90, 45, 66]))
@abdulateef
abdulateef / remove_duplicates.py
Last active December 9, 2018 11:27
Write a function which will take one string argument containing characters between a-z, and should remove all repeated characters (dupliactaes) in the string.
def remove_duplicates(string):
unique = ''.join(sorted(set(string)))
removed = len(string) - len(unique)
return unique, removed
print(remove_duplicates('accccount'))
class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = {}
def add_item(self, item_name, quantity, price):
self.total += price*quantity
self.items.update({item_name: quantity})
def remove_item(self, item_name, quantity, price):
@abdulateef
abdulateef / is_isogram.py
Last active December 9, 2018 11:28
Create a method using Python 2.7.x syntax called is_isogram that takes one argument, a word to test if it's an isogram. This method should return a tuple of the word and a boolean indicating whether it is an isogram. If the argument supplied is an empty string, return the argument and False: (argument, False). If the argument supplied is not a s…
def is_isogram(word):
#to get the type of word
if type(word)!=str:
#print error
raise TypeError("Argument should be a String")
#remove space
elif word.strip()=="":
return(word,False)
else:
#change to lower case
@abdulateef
abdulateef / LoadCSV.cs
Created July 2, 2017 07:53
how to load a CSV value from a text file with the column head into a datagridview using c#
public static string[,] LoadCsv(string filename)
{
//get the file text
string text = System.IO.File.ReadAllText(filename);
//split into Lines
text = text.Replace("\n", "\r");
string[] lines = text.Split(new char[] { '\r' }, StringSplitOptions.RemoveEmptyEntries);
// to get how many rows and columns
@abdulateef
abdulateef / columnCount.cs
Last active July 2, 2017 06:55
how to count the number of columns in a text file using c#
public static int columnCount(string filepath)
{
int numColumns = 0;
using(var reader = File.OpenText(filepath))
{
while(reader.ReadLine() != null )
{
string data = System.IO.File.ReadAllText(filepath);
// split the text
@abdulateef
abdulateef / consoleTerminate.cs
Last active July 2, 2017 06:58
how to terminate a console program on a button click
Console.WriteLine("Oop!! an error has occured");
Console.WriteLine("Press Enter to exit the program..." + "OR" + "Press Back Space to see the error");
ConsoleKeyInfo keyinfor = Console.ReadKey(true);
if(keyinfor.Key == ConsoleKey.Enter)
{
System.Environment.Exit(0);
}
else if(keyinfor.Key == ConsoleKey.Backspace)
{
Console.WriteLine(ex.Message);
@abdulateef
abdulateef / newLinesCount.cs
Last active July 2, 2017 06:57
how to count new line in a text file
public static int Rowcount(string filepath)
{
int lineCount = 0;
using(var reader = File.OpenText(filepath))
{
while(reader.ReadLine() != null)
{
lineCount++;
}