# Recursive directory traverse and append .png file to list

Tags

Python Developer’s Meetup Nepal #13 Question Solution

Q: Write a python script that recursively walks all sub-directories and searches all files with extension *.png or *.PNG and append them to the list.

Solution:

```import os

# list variable to store the .png and .PNG file
png_file_list = []

# Recursive function to traverse all the sub-dirctory and check for png files
def traverse_directory(dir_path):
for child in os.listdir(dir_path):
path = os.path.join(dir_path, child)
if os.path.isdir(path):
traverse_directory(path)
else:
# split the path and store file name only
file_name = path.split('\\')[-1]
# split the file name and store extension
file_name_extension = file_name.split('.')[-1]
# check if the file name has extension .png or .PNG
if(file_name_extension == 'png' or file_name_extension == 'PNG'):
png_file_list.append(file_name)

# Traverse current directory for files and folders
traverse_directory(".")

# Print the final list
print(png_file_list)
```

# Credit card Verification Haskell Code

```{- Convert positive integer to list of digit -}
{- Input : 12345 o/p : [1, 2, 3, 4, 5 -}
toList :: Integer -> [Integer]
toList 0 = []
toList x = toList (x `div` 10) ++ [x `mod` 10]

{- Reverse the integer after converting it to list -}
-- toDigitsRev :: Integer -> [Integer]
toListReverse :: Integer -> [Integer]
toListReverse xs = (reverse . toList) xs

{- Double every second number -}
doubleSecondEach :: [Integer] -> [Integer]
doubleSecondEach [] = []
doubleSecondEach (x:[]) = [x]
doubleSecondEach (x:y:xs) = x : (2*y) : doubleSecondEach xs

{- Sum the digit in the number we are sure there is one digit number or two digit number -}
sum' :: Integer -> Integer
sum' x = (x `mod` 10) + (x `div` 10)

{- sum all the element in list -}
{- when a element is [13,5,6] we have to add like this: 1 + 3 + 5 + 6 -}
sumDigits :: [Integer] -> Integer
sumDigits [] = 0
sumDigits [x] = sum' x
sumDigits (x:xs) = sum' x + sumDigits xs

{- Validate the credit card number -}
validate :: Integer -> Bool
validate x = if (sumDigits \$ doubleSecondEach \$ toListReverse x ) `mod` 10 == 0 then True else False
```

# Fixing Google Play Games plugin and Unity integration error – Android

Recently i was having multiple issues with google play games plugin integration in Unity. It was not working at all. After spending the couple of hours in it, i was able to fix the issue. This is a note to myself as well for others who are having trouble with the integration.

I was using Admob plugin along with Google Play Games plugin. In the process of fixing the error i got different kinds of error. Some error was due to the import while some was due to the Authentication issues from the play game service. I will discuss about these both.

# Starting Animation from random time frame Unity

Tags

Code snippet to start the animation at different time. Useful when there are many same animation running on screen and you want to make them random.

```Animator anim = GetComponent<Animator> ();
AnimatorStateInfo state = anim.GetCurrentAnimatorStateInfo (0);
anim.Play (state.fullPathHash, -1, Random.Range(0f,1f));
```

You can add this code in Start or Awake function and add to the GameObject with the Animator component.

# C# code to shuffle Array

Tags

```private T[] ShuffleArray<T>(T[] array) {
System.Random r = new System.Random ();
for(int i = array.Length; i > 0; i--) {
int j = r.Next (i);
T k = array [j];
array [j] = array [i - 1];
array [i - 1] = k;
}

return array;
}
```

Example:

## Int Array

```int[] intArray = new int[] {1,2,3,4};

intArray = ShuffleArray(intArray);
```

## Float Array

```float[] floatArray = new float[] {1.0f,2.0f,3.0f,4.0f};

floatArray = ShuffleArray(floatArray);
```