Programming

Credit card Verification Haskell Code

Sudeep Acharya
{- 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

Starting Animation from random time frame Unity

Sudeep Acharya
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

Sudeep Acharya
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);

Invisible Transparent Button in Unity3D

Sudeep Acharya
Today i was working on a project where i required to add the invisible button. I tried different methods like making button transparent, adding the image in the button. But then i came to know about this solution. I think that this is probably the best solution. Steps to add invisible button in Unity3D 1. Add the Button. (UI -> Button) 2. Edit the height and width of the button according to your fit.

Convert the list of images to PDF file and add watermark using python

Sudeep Acharya
Converting list of images to PDF in Ubuntu Let’s use ImageMagick tool. Normally it is installed in Ubuntu. If you need to install it then run: sudo apt-get install imagemagick Then you can convert it by using: convert image_1.jpg image_2.jpg output.pdf You can specify any numbers of images but the last argument must be a name of your PDF file along with extension (.pdf). Adding the watermark in the PDF file We use Python script for adding watermark to each page in the PDF file.

Understanding Decorators in Python

Sudeep Acharya
While learning Python in the beginning, i skipped the portion of the chapters on Decorators. After i worked on Flask and Django, I was introduced with the Decorators. Let us consider a simple flask app example: @app.route("/") def hello(): return "Hello World!" In this code the line with @ symbol is decorators. Decorators is one of the important feature in Python that you must learn to master it. It is not that hard, so let us learn it.

How to delete the commit history in github

Sudeep Acharya
Sometimes you may find deleting the commit history of your github project repository useful. You can easily delete the commit history by following the procedure below. It is always useful to keep the backup of your repository in your computer before removing all the commit history. Let us start by cloning a github project. I am cloning ‘myproject’, you clone yours. git clone https://github.com/acsudeep/myproject.git Since all the commit history are in the “.

URL Shortener with Django (Part 5) - Working with Templates and finishing up

Sudeep Acharya
Create folder ’templates’ inside ‘shortenersite’ and then inside ’templates’ create folder ‘shortenersite’. Inside “shortenersite/templates/shortenersite/” create ‘base.html’ and ‘index.html’. So now paste the following to them. [shortenersite/templates/shortenersite/base.html] {% raw %} <!DOCTYPE html> <html> <head> <title>{% block title %}Welcome to mysite{% endblock %}</title> {% load staticfiles %} <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="{% static 'shortenersite/css/custom.css' %}" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <script type="text/javascript"> $(function() { $('#submitButton').click(function() { $.ajax({ type: "POST", url: "/makeshort/", data: { 'url' : $('#url').

URL Shortener with Django (Part 4) - Working with Urls and views

Sudeep Acharya
Now we will be working with our actual URLs and Views of our site. Open up your “urlshortener/urls.py” and add the following code to it: [urlshortener/urls.py] from django.conf.urls import patterns, include, url from django.contrib import admin urlpatterns = patterns('', url(r'^admin/', include(admin.site.urls)), # if the URL pattern match /admin/ then open up admin panel url(r'', include('shortenersite.urls', namespace='shortenersite')), # if anything rather then /admin/ then it will look for shortenersite/urls ) Next, create a new file ‘urls.

URL Shortener with Django (Part 3) - Creating django app and model

Sudeep Acharya
Now, Let us create a Django app. From your project’s root directory run this command django-admin startapp shortenersite This will create a new folder ‘shortenersite’ inside our root directory which is actually a Django app. You can see the following files and folder inside shortenersite: migrations - Contains database migration file. admin.py - We will use this file to configure Django Admin settings for this app. models.py - Contains the model (generally model.