Posts

Showing posts from October, 2003

Bitset Count Set Bits Code Example

Example: bitwise count total set bits //WAP to find setbits (total 1's in binary ex. n= 5 => 101 => 2 setbits int count{}, num{}; cin >> num; while (num > 0) { count = count + (num & 1); // num&1 => it gives either 0 or 1 num = num >> 1; // bitwise rightshift } cout << count; //count is our total setbits

'Column Reference Is Ambiguous' When Upserting Element Into Table

Answer : From the docs, conflict_action specifies an alternative ON CONFLICT action. It can be either DO NOTHING, or a DO UPDATE clause specifying the exact details of the UPDATE action to be performed in case of a conflict. The SET and WHERE clauses in ON CONFLICT DO UPDATE have access to the existing row using the table's name (or an alias), and to rows proposed for insertion using the special excluded table. SELECT privilege is required on any column in the target table where corresponding excluded columns are read. So instead, try this per ypercubeᵀᴹ INSERT INTO accounts (id, token, affiliate_code) VALUES (value1, value2, value3) ON CONFLICT (id) DO UPDATE SET token = value2, affiliate_code = COALESCE(accounts.affiliate_code, excluded.affiliate_code); This answer helped me solve a slightly different ambiguous column problem. I have a table where we do daily roll-ups into the same table multiple times per day. We need to re-calculate the daily roll-up on an ho

Ansible: Restart Service Only If It Was Running

Answer : Register a variable when config is updated. Register another variable when you check if a service is running. Call service restart handler only if both variables are true. As for specifics of various OS, you could have conditional execution based on family/distribution from host facts. Ansible's service: module doesn't provide a mechanism for checking if a service is already running or not. Therefore you'll have to resort to using something like this via the shell: module to determine first if the service is running. Example Here I'm detecting whether WebSphere or Tomcat8 are running, and then restarting the appropriate service based on its status. --- # handles tomcat8 - name: is tomcat8 already running? shell: service tomcat8 status warn=false register: _svc_tomcat8 failed_when: _svc_tomcat8.rc != 0 and ("unrecognized service" not in _svc_tomcat8.stderr) ignore_errors: true - name: restart tomcat8 if running service: name=to

Angular 2 Get Current Route

Answer : Try this, import { Router } from '@angular/router'; export class MyComponent implements OnInit { constructor(private router:Router) { ... } ngOnInit() { let currentUrl = this.router.url; /// this will give you current url // your logic to know if its my home page. } } Try it import { Component } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; @Component({...}) export class MyComponent { constructor(private router:Router) { router.events.subscribe(event => { if (event instanceof NavigationEnd ) { console.log("current url",event.url); // event.url has current url // your code will goes here } }); } } Try any of these from the native window object. console.log('URL:' + window.location.href); console.log('Path:' + window.location.pathname); console.log('Host:' + window.location.host); console.log('Ho

Assign Value From Successful Promise Resolve To External Variable

Answer : Your statement does nothing more than ask the interpreter to assign the value returned from then() to the vm.feed variable. then() returns you a Promise (as you can see here: https://github.com/angular/angular.js/blob/master/src/ng/q.js#L283). You could picture this by seeing that the Promise (a simple object) is being pulled out of the function and getting assigned to vm.feed . This happens as soon as the interpreter executes that line. Since your successful callback does not run when you call then() but only when your promise gets resolved (at a later time, asynchronously) it would be impossible for then() to return its value for the caller. This is the default way Javascript works. This was the exact reason Promises were introduced, so you could ask the interpreter to push the value to you, in the form of a callback. Though on a future version that is being worked on for JavaScript (ES2016) a couple keywords will be introduced to work pretty much as you are ex

Android Custom Dropdown/popup Menu

Answer : Update : To create a popup menu in android with Kotlin refer my answer here. To create a popup menu in android with Java: Create a layout file activity_main.xml under res/layout directory which contains only one button. Filename: activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android

Bootstrap Hide Class Code Example

Example 1: display sm none Hidden on all .d-none Hidden only on xs .d-none .d-sm-block Hidden only on sm .d-sm-none .d-md-block Hidden only on md .d-md-none .d-lg-block Hidden only on lg .d-lg-none .d-xl-block Hidden only on xl .d-xl-none Visible on all .d-block Visible only on xs .d-block .d-sm-none Visible only on sm .d-none .d-sm-block .d-md-none Visible only on md .d-none .d-md-block .d-lg-none Visible only on lg .d-none .d-lg-block .d-xl-none Visible only on xl .d-none .d-xl-block Example 2: bootstrap display inline block <div class= "d-inline-block" ></div> Example 3: visibility hidden bootstrap 4 <div class= "visible" >...</div> <div class= "invisible" >...</div> Example 4: how to hide display in bootstrap <!-- Bootstrap 4 --> <div class= "d-none" ></div> <!-- Bootstrap 3 --> <!-- <div class= "hidden- { screen size } "></div> --> <div cl

Check Length Of String In Javascript Code Example

Example 1: js string length let str = "foo" ; console . log ( str . length ) ; // 3 Example 2: javascript string lentrh var myString = "string test" ; var stringLength = myString . length ; console . log ( stringLength ) ; // Will return 11 because myString // is 11 characters long... Example 3: javascript length var colors = [ "Red" , "Orange" , "Blue" , "Green" ] ; var colorsLength = colors . length ; //4 is colors array length var str = "bug" ; var strLength = str . length ; //3 is the number of characters in bug Example 4: length of string in javascript var str = "Hello World!" ; var n = str . length ; Example 5: javascript check string lenght let SomeString = "Example" ; console . log ( SomeString . length )

10 Factorial Code Example

Example 1: calculate factorial int factorial ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } Example 2: Factorial // METHOD ONE const factorialNumber = num = > { let factorials = [ ] for ( let i = 1 ; i <= num ; i ++ ) factorials . push ( i ) return factorials . reduce ( ( acc , curr ) = > acc * curr , 1 ) } // METHOD TWO const factorialNumber = num = > { let factorial = 1 , i = 1 while ( i <= num ) { factorial *= i ; i ++ } return factorial } // METHOD THREE function factorialNumber ( num ) { if ( num < 1 ) return 1 else return factorialNumber ( num - 1 ) * num } Example 3: factorial of 8 function getFactorial ( $ int ) { $factorial = 1 ; for ( $i = $ int ; $i > 1 ; $i -- ) { $factorial *= $i ; } echo "The factorial of " . $ int . "

Save Mp3 From Youtube Code Example

Example 1: youtube mp3 converter You can use WebTools , it's an addon that gather the most useful and basic tools such as a synonym dictionary , a dictionary , a translator , a youtube convertor , a speedtest and many others ( there are ten of them ) . You can access them in two clics , without having to open a new tab and without having to search for them ! - Chrome Link : https : //chrome.google.com/webstore/detail/webtools/ejnboneedfadhjddmbckhflmpnlcomge/ Firefox link : https : //addons.mozilla.org/fr/firefox/addon/webtools/ Example 2: youtube download mp3 I use https : //github.com/ytdl-org/youtube-dl/ as a Python CLI tool to download videos

Can Check Pip Version Code Example

Example: pip version command pip - - version

39 Cm In Inch Code Example

Example: cm to inch 1 cm = 0.3937 inch

Android Webview Flutter Example

Example: add web view in flutter A Flutter plugin that provides a WebView widget . On iOS the WebView widget is backed by a WKWebView ; On Android the WebView widget is backed by a WebView . Usage Add webview_flutter as a dependency in your pubspec . yaml file . You can now include a WebView widget in your widget tree . See the WebView widget's Dartdoc for more details on how to use the widget . Android Platform Views The WebView is relying on Platform Views to embed the Android ’s webview within the Flutter app . By default a Virtual Display based platform view backend is used , this implementation has multiple keyboard . When keyboard input is required we recommend using the Hybrid Composition based platform views implementation . Note that on Android versions prior to Android 10 Hybrid Composition has some performance drawbacks . Using Hybrid Composition To enable hybrid composition , set WebView . platform

Adjusting The Xcode IPhone Simulator Scale And Size

Image
Answer : With Xcode 9 - Simulator, you can pick & drag any corner of simulator to resize it and set according to your requirement. Look at this snapshot. Note: With Xcode 9.1+, Simulator scale options are changed. Keyboard short-keys : According to Xcode 9.1+ Physical Size ⌘ 1 command + 1 Pixel Accurate ⌘ 2 command + 2 According to Xcode 9 50% Scale ⌘ 1 command + 1 100% Scale ⌘ 2 command + 2 200% Scale ⌘ 3 command + 3 Simulator scale options from Xcode Menu : Xcode 9.1+: Menubar ▶ Window ▶ "Here, options available change simulator scale" ( Physical Size & Pixel Accurate ) Pixel Accurate : Resizes your simulator to actual (Physical) device's pixels, if your mac system display screen size (pixel) supports that much high resolution, else this option will remain disabled. Tip : rotate simulator ( ⌘ + ← or ⌘ + → ), if Pixel Accurate is disabled. It may be ena

Can I Take Legion On Tali's Loyalty Mission, Complete All Loyalty Missions (including His), And Still Save My Whole Crew?

Answer : You can perform all the loyalty missions, take Legion on Tali's loyalty mission, and save your entire crew if you do the following: Do everything but Tali's loyalty mission. Do the mission to obtain the Reaper IFF. Do Tali's loyalty mission. Do Legion's loyalty mission. At this point, you should actually be out of things to do, and you'll be wondering why the Collectors haven't attacked yet. Go click your galaxy map. This should trigger the Collector invasion. Once it is over, go straight through the Omega IV relay. You don't have anything else left to do anyway! I believe that you can make 3 jumps with the Normandy once you obtain the Reaper IFF before the collector attack. So it should be possible to obtain Legion, jump straight to Tali's loyalty mission, and then immediately do Legion's mission. Once the crew is abducted, its uncertain how many more missions you can do before half the crew dies, but general consensus seems