Skip to main content

Posts

Showing posts from November, 2023

How To Change The Owner Of A Directory Using Python?

Introduction: To change the permission of a directory or file in python you needed to know two thing your user name and user group you wanted to change I will going to describe in detail how to know your users on your computer and user group present on your computer, whether you are the window or linux or mac. Syntax : There are three main commands we can use are 1st command(main command) os.chown(directory_name,userid,groupid) Parameters: userid - user id is the parameter used to specify the user id. groupid - Is the group id used for specifying the group of the group id. 2nd command: usr.getpwnam(new_owner_user).pw_uid Above function returns user id. when username is passed as argument. 3rd Command: grp.getgrnam(new_owner_group).gr_gid Above function returns group id. when groupname is passed as argument. Sample Program : Python3 import os # Directory path you want to change ownership for directory_path = '/tmp/directory' # New owner user and group new_owner_user = 'ch

Declaring variables inside loops, good practice or bad practice?

  Q) can you analyze the following code which has the following error i\ error occured : Programming best Practise The declaration of  variable should be out side the loop for example while loop, for loop. for Example : Incorrect program: #include <bits/stdc++.h> using namespace std ; int main () {     int t;     cin >> t;     while (t -- )     {         int N;//variable is declared inside the while-loop         cin >> N;         cout << 2 * N << endl;     }     return 0 ; } This is the correct code which we can edit. #include <bits/stdc++.h> using namespace std ; int main () {     int t;     int N;// variable is declared outside the loop     cin >> t;     while (t -- )     {         cin >> N;         cout << 2 * N << endl;     }     return 0 ; } The decision of whether to declare a variable outside or inside a loop depends on the specific requirements and scope of the variable. Here are some consid

Split string into key-value pairs using C++

Introduction: In the realm of programming, the ability to parse strings efficiently is a valuable skill. It becomes particularly crucial when dealing with data in the form of key-value pairs. While there are libraries available for this task, let's explore a minimalistic approach using C++ without relying on external libraries. The Challenge: Consider a string `s` containing key-value pairs, where keys and values are separated by colons, and different pairs are delimited by commas. The goal is to extract and store these pairs without resorting to external libraries, showcasing a straightforward yet efficient solution. The Code Breakdown: #include <iostream> #include <cstring> #include <unordered_map> using namespace std ; int main ( int arg , char ** argv ) {     string s = "key1:value1,key2:value2,key3:value3," ;     unordered_map < string , string > kv ;     bool k = false ; // Flag to switch between keys and values     string ke

Is Dart easy for Beginners?

Lets look up few question on dart which help us to understand whether dart is easy or not. Introduction: Dart syntax are derived from java,c++ and mostly from javascript.It has c like syntax .Hence it is beginner friendly untill you understand the concept of classes. Hence here are few Questions on Dart programming language. Dart Question Question on Dart 1) For what iterable Collection in dart? Ans : The iterable Collection in dart :The  Iterable  class—for example  List  and  Set.  Iterables are basic building blocks for all sorts of Dart applications, and you’re probably already using them, even without noticing. This codelab helps you make the most out of them. 2 )What are type of loops in dart programming? Ans :/ for in loop ,switch Dart programming language supports several types of loops for controlling the flow of execution. Here are the main types of loops in Dart: For Loop: The for loop is used to iterate a specific number of times. It consists of an initialization, a cond

Tokenizing a string using c /c++

 Tokenizing a string using c /c++ Below is the code for c/c++ this a function works for me in c++. If any problem comment below.  the string str[] variable string is declared out side of function passed to function to store the tokens in the string array.remember to allocate sufficient or more space Required parameters for function  : char str[],string s[],const char t 1 parameter:  str[] is the string which you wanted to tokenize. 2 parameter : s[] is string arrray used to store the token.Declare it before function passed; 3 parameter:   t is char character at which the string should be tokenize(); Return value:  none  look  function  and  sample code  below. Function: void tokenize(char str[],string s[],const char t){int i=0; string token;int j=0;int len=strlen(str); for(int i=0;i< len;i++){ if (str[i]== t ){ s[j]=token;j++;token=" "; }else{token.push_back(str[i]);} if (i==len-1){s[j]=token;} } } sample code for testing(c++) #incl

classes in dart

Skeliton of classes in Dart classes in dartclass ClassName { // Fields (properties) go here type propertyName; // Constructor(s) go here ClassName( this .propertyName); // Constructor shorthand // Methods go here returnType methodName(parameters) { // Method body } } Example class Person { String name; int age; // Constructor Person( this .name, this .age); // Method void sayHello() { print ( "Hello, my name is $name, and I am $age years old." ); } } void main() { // Creating an instance of the Person class var person = Person( "Alice" , 30 ); // Accessing fields and calling methods print (person.name); // Output: Alice print (person.age); // Output: 30 person.sayHello(); // Output: Hello, my name is Alice, and I am 30 years old. } Extending a Class in Dart Certainly! In object-oriented programming, there are several important concepts related to classes and their behavior. Two of these