lintcode-solutions
  • LintCode 刷题笔记
  • 课程笔记
    • Backtracking
    • Binary Search
    • Divide & Conquer
    • Breadth First Search
    • Depth First Search
    • Linked List & Array
    • Two Pointers
    • Stack, Queue and Heap
    • Dynamic Programming
    • Two Pointers Advanced
    • Union Find and Trie
    • Dynamic Programming Advanced
  • 基础知识储备
    • Python
      • Stack and Queue
      • Namedtuple
      • Priority Queue
      • isinstance()
      • OrderedDict
      • set and frozen set
      • Counter
      • Heap
    • Bit Manipulation
    • Fenwick Tree or Binary Indexed Tree
    • Rabin-Karp Algorithm
    • Sort
      • Merge Sort
      • Quick Sort
      • Heap Sort
  • LintCode 练习题
    • Binary Search
  • OJ Review
    • Aug 7, 2018
    • Aug 8, 2018
    • Aug 9, 2018
    • Aug 13, 2018
    • Aug 17, 2018
    • Aug 19, 2018
    • Aug 24, 2018
    • Aug 26, 2018
    • Aug 27, 2018
    • Aug 29, 2018
    • Sep 1, 2018
    • Sep 2, 2018
    • Sep 3, 2018
    • Sep 4, 2018
    • Oct 28, 2018
    • Nov 13, 2018
Powered by GitBook
On this page
  • Python
  • dictionary get() Method
  • C++
  • abs()
  • std::tolower(charT c, const locale& loc)
  • std::string::erase()
  • std::string::substr()
  1. OJ Review

Aug 27, 2018

C++ Basics and Python

Python

dictionary get() Method

The method get() returns a value for the given key. If key is not available then returns default value None.

dict.get(key, default = None)
  • key − This is the Key to be searched in the dictionary.

  • default − This is the Value to be returned in case key does not exist.

dict = {'Name': 'Zabra', 'Age': 7}
print "Value : %s" %  dict.get('Age')
print "Value : %s" %  dict.get('Education', "Never")
Output:

Value : 7
Value : Never

C++

abs()

Returns the absolute value of parameter n .

/* abs example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* abs */

int main ()
{
  int n,m;
  n=abs(23);
  m=abs(-11);
  printf ("n=%d\n",n);
  printf ("m=%d\n",m);
  return 0;
}
Output

n=23
m=11

std::tolower(charT c, const locale& loc)

// tolower example (C++)
#include <iostream>       // std::cout
#include <string>         // std::string
#include <locale>         // std::locale, std::tolower

int main ()
{
  std::locale loc;
  std::string str="Test String.\n";
  for (std::string::size_type i=0; i<str.length(); ++i)
    std::cout << std::tolower(str[i],loc);
  return 0;
}

std::string::erase()

sequence (1)	string& erase (size_t pos = 0, size_t len = npos);
character (2)	iterator erase (const_iterator p);
range (3)	    iterator erase (const_iterator first, const_iterator last);
  1. character: Erases the character pointed by p.

  2. range: Erases the sequence of characters in the range [first,last).

string remove_rubbish(string s) {
    auto is_rubbish = [](char c) { 
        return ispunct(c) || isspace(c); 
    };
    
    // remove spaces and puctuations
    s.erase(std::remove_if(s.begin(), 
                           s.end(), 
                           is_rubbish), 
            s.end());
    return s;
}

std::string::substr()

string substr (size_t pos = 0, size_t len = npos) const;
  • pos

  • len

#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr(3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr(pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}
PreviousAug 26, 2018NextAug 29, 2018

Last updated 6 years ago

Converts parameter c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent, as determined by the facet of locale loc. If no such conversion is possible, the value returned is c unchanged.

Erases part of the , reducing its

sequence: Erases the portion of the string value that begins at the character position pos and spans len characters (or until the end of the string, if either the content is too short or if len is . Notice that the default argument erases all characters in the string (like member function ).

Returns a newly constructed object with its value initialized to a copy of a substring of this object. The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

Position of the first character to be copied as a substring. If this is equal to the , the function returns an empty string. If this is greater than the , it throws . Note: The first character is denoted by a value of 0 (not 1).

Number of characters to include in the substring (if the string is shorter, as many characters as possible are used). A value of indicates all characters until the end of the string.

ctype
string
length
string::npos
clear
string
string length
string length
out_of_range
string::npos