Greetings!! Here's a series of Cold Golf problems and solutions in Python as a part of 21 days of Code | Code Golf by Skillenza 

https://skillenza.com/challenge/21days-of-code

DAY - I

A Little Complex

Given n complex numbers each x + yi, where x is the real part and y is the imaginary part, find

For the need of a simple tabular Grid in your Angular JS application, one of the most basic and simple to use package is angular-ui-grid. The implementation is easy in many of the use cases, but when it comes to extend the limits of the ui-grid, life becomes tough.

One of the tricky task is to set the Grid height as dynamic, in other words to make the grid more flexible to number of rows and the grid size would depend on it.

Lets begin with the

Prerequisites:

  1. Angular JS: v1.4.2
  2. ui-grid-stable: v3.0.1
  3. Dataset: https://rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json
the Dataset contains 100 rows

In case you are new to ui-grid refer to the official doc: angular-ui-grid

Refer to the complete code in this plnkr: http://plnkr.co/edit/i5zAuUKz2RdJpuq5YKud

The Next Step-

Setting up all the things:

  1. Create 3 file:
    1. app.js - Contains your Angular JS code.
    2. index.html - Contains the UI HTML5 code.
    3. main.css - Our styling stuff goes here.
  2. Required imports in index.html are:
    1. JS Scripts 
      1. src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.js"
      2. src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-touch.js"
      3. src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular-animate.js"
      4. src="http://ui-grid.info/release/ui-grid-stable.js"
    2.  CSS Styling 
      1. href="http://ui-grid.info/release/ui-grid-stable.css"

Lets jump to the code:

Our base controller is in app.js

var app = angular.module('app', ['ngAnimate', 'ui.grid']);
 
app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
  $scope.maxRowToShow = 1000;
  $scope.gridOptions = {
      enableColumnMenus: false,
      enableHiding: false,
      enableSorting: false
    };
  $scope.gridOptions.columnDefs= [
      { field: 'name' },
      { field: 'gender' },
      { field: 'company', enableSorting: false }
    ];
 
  $scope.getTableHeight = getTableHeight;
 
  $http.get('https://rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
      setMinRowsToShow();
    });
 
  function setMinRowsToShow(){
    //This function is to set the rowContainer with minimum row limit to be stored 
    //in the visibleRowCache.
    //It handles and rogue cache or missed rows due to dynamically generated Table Height.
    $scope.gridOptions.minRowsToShow = Math.min($scope.gridOptions.data.length, $scope.maxRowToShow);
    $scope.gridOptions.virtualizationThreshold = $scope.gridOptions.minRowsToShow ;
  }
 
  function getTableHeight() {
    let rowHeight = 37; // row height
    let headerHeight = 48; // header height
    return {
      height: ($scope.gridOptions.data.length * rowHeight + headerHeight) + "px"
    };
  }
}]);
 

Dive in the Code:

  • maxRowToShow: This defines the upper limit of rows to be rendered
  • Initial Grid config: 
    • enableColumnMenus: false,
    • enableHiding: false,
    • enableSorting: false
  • Fetch and store data in scope.gridOptions.data
  • getTableHeight: Now here is where the magic happens-
    • we give the rowHeight and headerHeight as defined in our css or as per your requirement
    • Calculation of height attribute using the below formula:-
(Number of data rows) * rowHeight + headerHeight [in px]

  • columnDefs: Contains the meta data for your grid column, like name, specific formatting etc.
  • Lastly, the one which is bit confusion is setMinRowsToShow, which we will cover once i have shown you the output
The initial output looks like this without dynamic height:

Now with the Dynamic Height:
UI Grid with dynamic height, but we can see a large white gap between the last row and footer

As you can see in the above image there is a large white gap between the last row and footer, this is because of below reason:
  1. grid.renderContainers.body.visibleRowCache does not refresh the renderedRows arry when splicing in more data. That means when the number of rows are more in conjunction with the grid height, the module is unable to refresh renderedRows and we see only 4 rows
    Here are the counts:
    1. renderedRows: 4
    2. visibleRowCache: 100
    3. visibleColumnCache: 3
    4. viewportAdjuster: 1
  2. The above issue of only 4 rows in renderedRows array is due to the fact that ui-grid does not completely support dynamic height feature.
  3. But there is this parameter which comes to our rescue - virtualisationThreshold
  4. Lets fix the above issue gracefully:
$scope.gridOptions.minRowsToShow = Math.min($scope.gridOptions.data.length, $scope.maxRowToShow);
$scope.gridOptions.virtualizationThreshold = $scope.gridOptions.minRowsToShow ;
In Above code we will set gridOptions.virtualizationThreshold to the number of data rows or the max rows as configured, this was the ui-grid rendering mechanism knows that how many rows it need to check for to refresh renderedRows.

After the above changes our issue is resolved and we see the output like below:

The Final Output


Hope you find this article useful, let me know the feedback or any queries below as comments.



0

Add a comment

Hey guys, Hope you all are doing great. 

Algorithm To Convert Post-fix Expression into Infix expression:-

Scan the Post-fix String from Left to Right. If the character is an Operand, then Push it on to the deque.
Hey guys, Hope you all are doing great.

As the first part of this series of Python package and installation we will cover pip vs Conda

pip vs Conda

pip

Python packages only. Compiles everything from source.
Hey guys,

Hope you all are doing great. I love Code Golfing, what it brings out in you is the hidden concepts of a programming language and especially the push to implement same thing in numerous ways.
Hey guys, Hope you all are doing great.

I have always found Regular expressions or REGEX quite amazing. You can solve your string related problem in just some simple magic of patterns.

Our brain works very well to identify the pattern in our day to day life.
Hey guys, Hope you all are doing great. I am back with Some Python Programming tips for

CODE GOLF Challenges.
Hey guys, Hope you all are doing great. I am back with Some Python Programming tips for

CODE GOLF Challenges.
Hey guys, Hope you all are doing great. I am back with another problem of Project Euler on

Project Euler #11: Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in bold.
Hey guys, Hope you all are doing great.

Today i came across an intruding method, which is not used in generally but is very effective and we encounter its application a lot of .

That's 

Tabibitosan method

Tabibitosan: is a Japanese word which literally means "Pilgrim".
Hey guys, 

Hope you all are doing great. I am back with another problem of Project Euler on

The sum of the primes below 10 is 2+3+5+7=17

Find the sum of all the primes not greater than given N.

Input Format

The first line contains an integer T i.e. number of the test cases.
Hey guys, Hope you all are doing great. I am back with a new domain SQL. So the problem goes like, you need to print prime numbers less than 1000.

Seems easy right!! Just use a for loop check for the list of natural numbers for prime condition whether they are only divisible by 1 and itself.
Hey guys,

Hope you all are doing great. I am back with another problem of Project Euler on

Project Euler #8: Largest product in a series

Find the greatest product of KK consecutive digits in the NN digit number.

Input Format

First line contains TT that denotes the number of test cases.
Hey guys,

Hope you all are doing great. I am back with another problem of Project Euler

Project Euler #2: Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms.
Hey guys, 

Hope you all are doing great. I am back with another problem of Project Euler on

Project Euler #6: Sum square difference

The sum of the squares of the first ten natural numbers is, 12+22+...+102=385. The square of the sum of the first ten natural numbers is, (1+2+⋯+10)2=552=3025.
Hey guys, 

Hope you all are doing great. I am back with another problem of Project Euler on

Project Euler #5: Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
Hey guys, 

Hope you all are doing great. I am back with another problem of Project Euler on

Project Euler #3: Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of a given number N?

Input Format

First line contains T, the number of test cases.
Hey Fellas!! So its time to roll on.

Mathematical series are very interesting in there own ways. I find them interesting as they tickles your brain to know more.

That you try to find the end. Counting on your fingers, you will get bored or either get tired.
One of the Popular contest in Hackerrank based on a famous site ProjectEuler.net is 

Project Euler+ 

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve.
Loading